繁体   English   中英

UnboundLocalError:分配前已引用局部变量“ endProgram”

[英]UnboundLocalError: local variable 'endProgram' referenced before assignment

我见过多个关于同一问题的帖子,但没有找到满足我问题的答案。 我是从类分配中提供的伪代码中编写此代码的,这通常是程序中的常见错误,通常我会采取另一种方法来规避此问题。 对于这个程序,尽管我需要遵循确切的伪指令,但让我感到困惑。 所以这里去

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”,位于main()中的第96行,文件“ C:/ Users / edigi / Desktop / FSW / COP 1000 / YumYumBurger.py”,第15行,位于主要的clarifyVariables(endProgram,endOrder,totalBurger,totalFry,totalSoda,total,tax,UnboundLocalError:分配前引用的本地变量“ endProgram”

我似乎无法避免这个问题,只是对我做错的事情的解释对我来说是很好的。

main()第一行是这样的:

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

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

但是在main() ,这些变量都不存在。 因此,您得到了错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM