将值从输入导入到另一个函数

时间:2013-11-08 12:28:49

标签: python

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成为一个全局变量,我可以让它工作, 但我想把它放在一个函数中,这样我就可以改变它出现的顺序 在该计划中。

3 个答案:

答案 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   
祝你好运