0
votes

I've seen multiple posts about this same issue but no answers that satisfy my issue. I wrote this code from a pseudo code provided as part of my class assignment, this generally seems to be a common error in my programs and normally I circumvent the issue by taking another route. for this program though i'm required to follow the exact pseudo and its left me boggled. so here goes

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

obviously the rest of the program is much longer but I feel this is the only relevant information

the full error displays as follows:

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

I cant seem to avoid this issue, just an explanation of what i'm doing wrong will be fine for me.

1
You are fundamentally not understanding scope. Your variables in declareVariables, for example, are all local. You do nothing with the values, and they are simply discarded when the function terminates. You try to call declareVariables in main by passing as arguments variables that do not exist. - juanpa.arrivillaga
I suggest reading through and understanding the following: nedbatchelder.com/text/names.html it should set you straight - juanpa.arrivillaga
I appreciate the reading material, I generally define my variables first then call my functions. The assignment is to write the program exactly as my professors pseudo code displays it. I think ill have to rewrite it and see how he feels about it. - Eric Digiorgio

1 Answers

0
votes

The very first line in main() is this:

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

This means you are calling the declareVariables() function with the given argument values.

But at that point in main(), none of those variables exist. So, you got the error.