UnboundLocalError:在assignmen之前引用了局部变量“endProgram”

2024-09-26 18:06:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我看过很多关于这个问题的帖子,但是没有一个能满足我的问题的答案。我用类赋值中提供的伪代码编写了这段代码,这在我的程序中通常是一个常见的错误,通常我通过另一个途径来规避这个问题。对于这个程序,虽然我被要求遵循精确的伪码,它让我困惑不解。所以就这样

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

显然剩下的程序要长得多,但我觉得这是唯一相关的信息

完整错误显示如下:

Traceback (most recent call last): File "C:/Users/edigi/Desktop/FSW/COP 1000/YumYumBurger.py", line 96, in main() File "C:/Users/edigi/Desktop/FSW/COP 1000/YumYumBurger.py", line 15, in main declareVariables(endProgram, endOrder, totalBurger, totalFry, totalSoda, total, tax, UnboundLocalError: local variable 'endProgram' referenced before assignment

我似乎无法回避这个问题,只要解释一下我做错了什么就好了。在


Tags: noreturntotaloptiontaxentersubtotalendprogram
1条回答
网友
1楼 · 发布于 2024-09-26 18:06:16

main()中的第一行是:

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

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

但在main()中,这些变量都不存在。所以,你搞错了。在

相关问题 更多 >

    热门问题