【问题标题】:I am having trouble getting my function to return a global varriable我无法让我的函数返回全局变量
【发布时间】:2014-02-18 02:11:17
【问题描述】:

Traceback(最近一次调用最后一次): 文件“C:\Users\Mitch\Documents\lab3.py”,第 18 行,在 主要的() 文件“C:\Users\Mitch\Documents\lab3.py”,第 15 行,在 main 找折扣(数量) 文件“C:\Users\Mitch\Documents\lab3.py”,第 2 行,在 finddiscount 中 如果数量 >= 1 且数量

def finddiscount(discount):
    if quantity >= 1 and quantity <= 9:
        discount = "0%"
    elif quantity >= 10 and quantity <= 19:
        discount = "20%"
    elif quantity >= 20 and quantity <= 49:
        discount = "30%"
    elif quantity >= 50 and quantity <= 99:
        discount = "40%"
    elif quantity >= 100:
        discount = "50%"
    print (discount)    
def main():
    quantity = int(input("How many packages where purchased?"))
    finddiscount(quantity)
    price = float(input("How much is each item?"))
    return
main()

【问题讨论】:

  • quantitymain 的范围内,而不是在全局范围内。

标签: python django function if-statement


【解决方案1】:

试试

def finddiscount(quantity):   # not discount!

【讨论】:

    猜你喜欢
    • 2017-06-09
    • 2020-09-11
    • 1970-01-01
    • 2015-11-28
    • 2020-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    相关资源
    最近更新 更多