Alec_ccc
Alec_ccc

Reputation: 83

How do I get or initialize the variable that I want?

I'm trying to write a simple program, but I meet a problem that the program does not output the given variable "tax" in the end.

def main():
    # define and initialize constants and variables
    menu1 = 6
    menu2 = 2.5
    menu3 = 1.25
    menu4 = 3.75
    choose = total = 0
    tax = total*0.06
    
    # display welcome
    print("Welcome to Yum Yum Snack Bar!")
    try:
        while choose != 5:
            print("\nPlease choose from the following menu:")
            print("1) Personal Pizza $6.00")
            print("2) Pretzel $2.50")
            print("3) Chips $1.25")
            print("4) Hot Dog $3.75")
            print("5) Exit ")
            choose = int(input("\nEnter your choice here: "))
            if choose == 1:
                total += menu1
            elif choose == 2:
                total += menu2
            elif choose == 3:
                total += menu3
            elif choose == 4:
                total += menu4
            elif choose == 5:
                continue
            else:
                print("Invalid choice. Must choose 1 – 5. Try again.")
            print("Current total: $",total)
    except:
        print("Invalid choice. Must choose 1 – 5. Try again.")
        main()
    print("Current total: $",total)
    print("Sales tax: $",tax)
    print("Total Bill: $",total+tax)
    print("Have a nice day!")
main()

Upvotes: 2

Views: 116

Answers (4)

klv0000
klv0000

Reputation: 174

I guess your problem is solved.Also start using python dictionary instead of using lots of if else statements

def main():
    # define and initialize constants and variables
    menu1 = 6
    menu2 = 2.5
    menu3 = 1.25
    menu4 = 3.75
    choose = total = 0
    tax = total*0.06
    
    # display welcome
    print("Welcome to Yum Yum Snack Bar!")
    try:
        while choose != 5:
            print("\nPlease choose from the following menu:")
            print("1) Personal Pizza $6.00")
            print("2) Pretzel $2.50")
            print("3) Chips $1.25")
            print("4) Hot Dog $3.75")
            print("5) Exit ")
            choose = int(input("\nEnter your choice here: "))
            price_dict = {
            1: menu1,
            2: menu2,
            3: menu3,
            4: menu4
            }
          
            if 0 < choose < 5:
                total += price_dict[choose]
            else:
                if choose == 5:
                    continue
                else:        
                    print("Invalid choice. Must choose 1 – 5. Try again.")
            print("Current total: $",total)
    except:
        print("Invalid choice. Must choose 1 – 5. Try again.")
        main()
    tax = total*0.06

    print("Current total: $",total)
    print("Sales tax: $",tax)
    print("Total Bill: $",total+tax)
    print("Have a nice day!")
main()

Upvotes: 1

Moritz Ortwein
Moritz Ortwein

Reputation: 19

Now your tax is always 0. You have to calculate the tax at the end. Like this:

def main():
    # define and initialize constants and variables
    menu1 = 6
    menu2 = 2.5
    menu3 = 1.25
    menu4 = 3.75
    choose = total = 0

    # display welcome
    print("Welcome to Yum Yum Snack Bar!")
    try:
        while choose != 5:
            print("\nPlease choose from the following menu:")
            print("1) Personal Pizza $6.00")
            print("2) Pretzel $2.50")
            print("3) Chips $1.25")
            print("4) Hot Dog $3.75")
            print("5) Exit ")
            choose = int(input("\nEnter your choice here: "))
            if choose == 1:
                total += menu1
            elif choose == 2:
                total += menu2
            elif choose == 3:
                total += menu3
            elif choose == 4:
                total += menu4
            elif choose == 5:
                continue
            else:
                print("Invalid choice. Must choose 1 – 5. Try again.")
            print("Current total: $", total)

    except:
        print("Invalid choice. Must choose 1 – 5. Try again.")
        main()
    print("Current total: $", total)
    tax = total * 0.06
    print("Sales tax: $", tax)
    print("Total Bill: $", total + tax)
    print("Have a nice day!")


main()

Upvotes: 1

maha
maha

Reputation: 643

when you initialized tax, you gave it a value of 0 because total*0.06 at that point equaled zero.

python goes line by line so the variable "tax" didn't change its value for the whole code, you only changed "total".

so to get the tax, you should calculate it again.

print("Current total: $",total)
tax=0.06*total
print("Sales tax: $",tax)
print("Total Bill: $",total+tax)
print("Have a nice day!")

Upvotes: 4

Aparna
Aparna

Reputation: 422

Here the value of total gets updated but the tax is calculated before the check as given below, hence tax will output tax = total*0.06 where initially total=0 Please try this:

def main():
    # define and initialize constants and variables
    menu1 = 6
    menu2 = 2.5
    menu3 = 1.25
    menu4 = 3.75
    choose = total = tax = 0
    # display welcome
    print("Welcome to Yum Yum Snack Bar!")
    try:
        while choose != 5:
            print("\nPlease choose from the following menu:")
            print("1) Personal Pizza $6.00")
            print("2) Pretzel $2.50")
            print("3) Chips $1.25")
            print("4) Hot Dog $3.75")
            print("5) Exit ")
            choose = int(input("\nEnter your choice here: "))
            if choose == 1:
                total += menu1
            elif choose == 2:
                total += menu2
            elif choose == 3:
                total += menu3
            elif choose == 4:
                total += menu4
            elif choose == 5:
                continue
            else:
                print("Invalid choice. Must choose 1 – 5. Try again.")
            print("Current total: $",total)
    except:
        print("Invalid choice. Must choose 1 – 5. Try again.")
    tax = total*0.06
    print("Current total: $",total)
    print("Sales tax: $",tax)
    print("Total Bill: $",total+tax)
    print("Have a nice day!")
main()

Upvotes: 3

Related Questions