UnboundLocalError:局部变量' endProgram'在分配之前引用

时间:2018-02-05 03:47:32

标签: python variables variable-assignment local

我已经看过多篇关于同一问题的帖子,但没有满足我的问题的答案。我从作为我的类赋值的一部分提供的伪代码编写此代码,这通常似乎是我的程序中的常见错误,通常我通过采取另一种途径来规避问题。对于这个程序,虽然我需要遵循确切的伪,其左边我可笑。所以这里去了

def main():

declareVariables(endProgram, endOrder, totalBurger, totalFry, totalSoda, total, tax,
                 subtotal, option, burgerCount, fryCount, sodaCount)

while endProgram == 'no' or endProgram == 'No':

    resetVariables(totalBurger, totalFry, totalSoda, total, tax, subtotal)

    while endOrder == 'no' or endOrder == 'No':
        print("Enter 1 for Yum Yum Burger")
        print("Enter 2 for Grease Yum Fries")
        print("Enter 3 for Soda Yum")
        option = input("Please make your selection: ")
        if option == 1:
            getBurger(totalBurger, burgerCount)
            return option
        elif option == 2:
            getFry(totalFry, fryCount)
            return option
        elif option == 3:
            getSoda(totalSoda, sodaCount)
            return option

        endOrder = input("Do you want to end your order?(Enter no to add more items: ")
        return endOrder


    calcTotal(burgerTotal, fryTotal, sodaTotal, total, subtotal, tax)
    printReceipt(total)

    endProgram = input("Do you want to end the program?(Enter no to process a new order)")
    return endProgram

def declareVariables(endProgram, endOrder, totalBurger, totalFry, totalSoda, total, tax,
                 subtotal, option, burgerCount, fryCount, sodaCount):
endProgram = 'no'
endOrder = 'no'
totalBurger = 0
totalFry = 0
totalSoda = 0
total = 0
tax = 0
subtotal = 0
option = 0
burgerCount = 0
fryCount = 0
sodaCount = 0

显然程序的其余部分要长得多,但我觉得这是唯一的相关信息

完整错误显示如下:

  

追踪(最近一次通话):     文件" C:/ Users / edigi / Desktop / FSW / COP 1000 / YumYumBurger.py",第96行,in       主要()     文件" C:/ Users / edigi / Desktop / FSW / COP 1000 / YumYumBurger.py",第15行,主要       declareVariables(endProgram,endOrder,totalBurger,totalFry,totalSoda,total,tax,   UnboundLocalError:局部变量' endProgram'在分配前引用

我似乎无法避免这个问题,只是解释我做错了什么对我来说没问题。

1 个答案:

答案 0 :(得分:0)

main()中的第一行是:

declareVariables(endProgram, endOrder, totalBurger, totalFry, totalSoda, total, tax,
                 subtotal, option, burgerCount, fryCount, sodaCount)

这意味着您使用给定的参数值调用declareVariables()函数。

但是在main()的那一点上,这些变量都不存在。所以,你得到了错误。

相关问题