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'获取值时出现问题 功能找到折扣。如果我让userinput成为一个全局变量,我可以让它工作, 但我想把它放在一个函数中,这样我就可以改变它出现的顺序 在该计划中。
答案 0 :(得分: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
返回中获得其价值。
答案 1 :(得分:1)
您需要从userinput
返回值,并可能转换为int
def userinput():
return int(input('Enter the amount of bags to be purchased: '))
答案 2 :(得分:0)
您应该从用户输入函数
def userinput():
amount_purchased = input("Enter the amount of bags to be purchased: ")
return amount_purchased
祝你好运