Veyronvenom1200
Veyronvenom1200

Reputation: 95

Why am i getting this error "name is not defined" when it is?

Why am i getting this error? The picture has the details. I need to get the _spent values to print the proper amount of times. So, say it runs through the loop 3 times, I need it to print 3. I think that is where the 1s are coming from. I don't like it! enter image description here

pennies = 10
nickels = 10
dimes = 10
quarters = 10

print("\nWelcome to change-making program.")
in_str = input("\nEnter the purchase price (xx.xx) or `q' to quit: ")

while in_str.lower() != 'q':
    dollar_str, cents_str = in_str.split(".")

    if in_str.lower() == 'q':
        quit()

    in_int = int(float(in_str) * 100)

    if in_int < 0:
        print("Error: purchase price must be non-negative.")
        in_str = input("\nEnter the purchase price (xx.xx) or `q' to quit: ")

    if in_int > 0:
        payment = input("\nInput dollars paid: ")
        payment_int = int(float(payment) * 100)
        change = payment_int - in_int

        #determines if there payment input
        if payment_int < in_int:
            print("Error: Insufficient payment.")
            payment = input("\nInput dollars paid: ")
            payment_int = int(float(payment) * 100)


        if change == 0:
            print("No change.")

        #determines how many quarters, dimes, nickels, and pennies are left
        while change >= 25 and quarters > 0:
            change = change - 25
            quarters_spent = 0
            quarters_spent += 1
            quarters = quarters - quarters_spent
        print(quarters_spent)

        while change >= 10 and dimes > 0:
            change = change - 10
            dimes_spent = 0
            dimes_spent += 1
            dimes = dimes - dimes_spent
        print(dimes_spent)

        while change >= 5 and nickels > 0:
            change = change - 5
            nickels_spent = 0
            nickels_spent += 1
            nickels = nickels - nickels_spent
        print(nickels_spent)

        while change >= 1 and pennies > 0:
            change = change - 1
            pennies_spent = 0
            pennies_spent += 1
            pennies = pennies - pennies_spent

        if quarters == 0 and dimes == 0 and nickels == 0 and pennies == 0:
            print("Error: ran out of coins.")
            quit()

        print("\nCollect Payment Below:")
        print(10 - quarters, "Quarters")

        print("\nStock: ", quarters, "Quarters, ", dimes, " Dimes, ", nickels, " Nickels, ", pennies, " Pennies ")

        in_str = input("\nEnter the purchase price (xx.xx) or `q' to quit: ")

        pennies = pennies
        nickels = nickels
        dimes = dimes
        quarters = quarters

Upvotes: 1

Views: 1477

Answers (2)

Steve Deng Zishi
Steve Deng Zishi

Reputation: 128

You only defined and initialized this variable nickels_spent inside the while loop

However, if the condition is not met, the program will skip the loop and jump to execute this print(nickels_spent) statement where this variable is not yet defined.

You can either

  1. Move the print statement into the while loop

Or

  1. Define and initialize this variable outside the while loop.

Depends on the purpose of your program

Upvotes: 0

Ofer Arial
Ofer Arial

Reputation: 1139

This error implies that you didn't define the value nickels_spent before trying to use it.

I guess the error is in this line: print (nickels_spent).

What is probably happening is that the while statement condition that is used to assign a value to that variable is not true when you try to run it, so it wasn't defined but you still try to use it.

Try debugging before that while loop to see what exactly happens there.

Upvotes: 1

Related Questions