0
def userinput():
    amount_purchased = input('Enter the amount of bags to be purchased: ')

#The amount entered is compared and the rate of discount is printed
def find_discount(amount_purchased):
    amount_purchased = userinput()
    if amount_purchased <=25:
        discount_rate=0.0
        print('The discount rate is 0%')
    elif amount_purchased <=50:
        discount_rate=0.05
        print('The discount rate is 5%')
    elif amount_purchased <=99:
        discount_rate=0.1
        print('The discount rate is 10%')
        print('The discount rate is 10%')

嗨,我在尝试从“用户输入”中获取值以显示在功能查找折扣中时遇到问题。如果我让 userinput 成为一个全局变量,我可以让它工作,但我想把它放在一个函数中,这样我就可以改变它在程序中出现的顺序。

4

3 回答 3

2

您需要从函数返回值:

def userinput():
    amount_purchased = input('Enter the amount of bags to be purchased: ')
    return amount_purchased

也可以input()直接返回结果:

def userinput():
    return input('Enter the amount of bags to be purchased: ')

另外:为什么find_discount接收amount_purchased作为参数?在我看来,无论回报如何,amount_purchased都会获得它的价值。userinput

于 2013-11-08T12:30:46.653 回答
1

您需要从返回值userinput并可能转换为int

def userinput():
    return int(input('Enter the amount of bags to be purchased: '))
于 2013-11-08T12:31:14.420 回答
0

您应该从 userinput 函数返回值
所以将您的函数更改为:

def userinput():
        amount_purchased = input("Enter the amount of bags to be purchased: ")
        return amount_purchased   

祝你好运

于 2013-11-08T12:43:54.640 回答