可能的范围问题,无法正确计算

时间:2016-02-14 10:41:40

标签: python python-3.x scope

我过去三四个小时一直试图调试和研究这个问题,但仍无法弄清楚。这很可能很简单,因为我不知道该怎么做。这是我的代码:

def main():
    endProgram = "no"
    endOrder = "no"
    totalFry = 0.0
    totalSoda = 0.0
    total = 0.0
    tax = 0.0
    subtotal = 0.0
    option = 0
    burgerCount = 0
    fryCount = 0
    sodaCount = 0
    while(endProgram == "no"):
        resetVariables(totalBurger, totalFry, totalSoda, total, tax, subtotal)
        while(endOrder == "no"):
            option =int(input("Enter 1 for Yum Yum Burger\nEnter 2 for Grease Yum Fries\nEnter 3 for Soda Yum"))
            if(option == 1):
                getBurger(totalBurger, burgerCount)
            elif(option == 2):
                getFry(totalFry, fryCount)
            elif(option == 3):
                getSoda(totalSoda, sodaCount)
            endOrder = input("Do you want to end your order?")
        calcTotal(totalBurger, totalFry, totalSoda, total, subtotal, tax)
        printReceipt(total)
        endProgram = input("Do you want to end the program?")

def resetVariables(totalBurger, totalFry, totalSoda, total, tax, subtotal):
    totalBurger = 0
    totalFry = 0
    totalSoda = 0
    total = 0.0
    tax = 0.0
    subtotal = 0.0
    return(totalBurger, totalFry, totalSoda, total, tax, subtotal)

def getBurger(totalBurger, burgerCount):
    burgerCount = float(input("Enter the number of burgers you want.\n\t"))
    totalBurger = totalBurger + burgerCount * .99
    return(totalBurger, burgerCount)

def getFry(totalFry, fryCount):
    fryCount = float(input("Enter the number of fries you want.\n\t"))
    totalFry = totalFry + fryCount * .79
    return(totalFry, fryCount)

def getSoda(totalSoda, sodaCount):
    sodaCount = float(input("Enter the number of sodas you want.\n\t"))
    totalSoda = totalSoda + sodaCount * 1.09
    return(totalSoda, sodaCount)

def calcTotal(totalBurger, totalFry, totalSoda, total, subtotal, tax):
    subtotal = totalBurger + totalFry + totalSoda
    tax = subtotal * .6
    total = subtotal + tax
    return(totalBurger, totalFry, totalSoda, total, subtotal, tax)

def printReceipt(total):
    print("Your total is: $", total)
    return(total)

main()

我确实有一切正确缩进,我只是累了,懒得编辑它。每当我运行它时,我输入所有内容,它只是说我的总数是0.0美元,无论如何。回溯并不是一个错误,所以我不能给它。感谢您提供帮助的任何事情。如果您认为此帖子之前已被回复,发布您认为可能有用的信息。

1 个答案:

答案 0 :(得分:1)

main函数中,您调用calcTotal但不存储返回的结果。您可以尝试在total结束时返回calctotal,然后在main中回复

total = calcTotal(totalBurger, totalFry, totalSoda, total, subtotal, tax)
相关问题