0

I am making a supermarket interface, and my program works fine if I run without going back. However, if userchoice = 2, I can't go back to userchoice = 1. I can only go to 2 or the options after it. I think there could be something wrong with my while loop.

apples = 0
mangoes = 0
print("Welcome to the CS110 Supermarket!")
print("1. Potatoes ($0.75 per potato")
print("2. Tomatoes ($1.25 per tomato")
print("3. Apples ($0.5 per apple")
print("4. Mangoes ($1.75 per mango")
print("5. Checkout")
user_choice = int(input())
total = total + potatoes*0.75 + tomatoes*1.25 + apples*0.5 + mangoes*1.75
while user_choice == 1:
    potatoes = int(input("How many potatoes?"))
    total = total + potatoes*0.75
    print("The total cost is $", total)
    print("Welcome to the CS110 Supermarket!")
    print("1. Potatoes ($0.75 per potato")
    print("2. Tomatoes ($1.25 per tomato")
    print("3. Apples ($0.5 per apple")
    print("4. Mangoes ($1.75 per mango")
    print("5. Checkout")
    user_choice = int(input())
while user_choice == 2:
    tomatoes = int(input("How many tomatoes?"))
    total = total + tomatoes*1.25
    print("The total cost is $", total)
    print("Welcome to the CS110 Supermarket!")
    print("1. Potatoes ($0.75 per potato")
    print("2. Tomatoes ($1.25 per tomato")
    print("3. Apples ($0.5 per apple")
    print("4. Mangoes ($1.75 per mango")
    print("5. Checkout")
    user_choice = int(input())
while user_choice == 3:
    apples=int(input("How many apples?"))
    total = total + apples*0.5
    print("The total cost is $", total)
    print("Welcome to the CS110 Supermarket!")
    print("1. Potatoes ($0.75 per potato")
    print("2. Tomatoes ($1.25 per tomato")
    print("3. Apples ($0.5 per apple")
    print("4. Mangoes ($1.75 per mango")
    print("5. Checkout")
    user_choice = int(input())
while user_choice == 4:
    mangoes = int(input("How many mangoes?"))
    total = total + mangoes*1.75
    print("The total cost is $",total)
    print("Welcome to the CS110 Supermarket!")
    print("1. Potatoes ($0.75 per potato")
    print("2. Tomatoes ($1.25 per tomato")
    print("3. Apples ($0.5 per apple")
    print("4. Mangoes ($1.75 per mango")
    print("5. Checkout")
    user_choice=int(input())
if user_choice == 5:
    print("The total cost is $",total)
    pay = float(input("Please enter an amount to pay for the fruits and vegetables: "))
    while pay < total:
        pay = float(input("Please enter an amount more than payment: "))
    change = pay - total
    print("Your change will be $", change)
    d5 = change // 5
    d1 = (change % 5) // 1
    quarter = ((change % 5) % 1) // 0.25
    dime = (((change % 5) % 1) % 0.25) // 0.1
    nickel = ((((change % 5) % 1) % 0.25) % 0.1) // 0.05
    penny = (((((change % 5) % 1) % 0.25) % 0.1) % 0.05) // 0.01
    print("Give the customer", d5, "$5 note,", d1, "$1 note,", quarter, "quarter,", dime, "dime,", nickel,"nickel,", penny, "penny.")
1
  • 5
    Try one while loop with an if for each of your options. Commented Dec 7, 2016 at 20:47

2 Answers 2

2

You're using more whiles than you need. rather than a while for each option, use only one loop, and several ifs for options:

while True:
  user_choice=int(input())
  if user_choice == 1:
    ...
  if user_choice == 2:
    ...
  ...
  if user_choice == 5:
    break # exit the loop with break
Sign up to request clarification or add additional context in comments.

Comments

-1

You could use an infinite loop while putting the user_input at the top of the loop. Then, you just have to use if conditions. You are currently using one loop per parameter. This is not going to work since you can't get out that loop to process an other choice.

while True:
    print("Welcome to the CS110 Supermarket!")
    print("1. Potatoes ($0.75 per potato")
    print("2. Tomatoes ($1.25 per tomato")
    print("3. Apples ($0.5 per apple")
    print("4. Mangoes ($1.75 per mango")
    print("5. Checkout")

    user_choice = int(input())
    if user_choice==1:
        potatoes=int(input("How many potatoes?"))
        total=total+potatoes*0.75

    elif user_choice==2:
        tomatoes=int(input("How many tomatoes?"))
        total=total+tomatoes*1.25

    elif user_choice==3:
        apples=int(input("How many apples?"))
        total=total+apples*0.5


    elif  user_choice==4:
        mangoes=int(input("How many mangoes?"))
        total=total+mangoes*1.75


    if user_choice==5:
        print("The total cost is $",total)
        pay=float(input("please enter an amount to pay for the fruits and vegetables: "))
        while pay<total:
            pay=float(input("please enter an amount more than payment: "))
        change=pay-total
        print("your change will be $",change)
        d5 = change // 5
        d1 = (change % 5) // 1
        quarter = ((change % 5) % 1) // 0.25
        dime = (((change % 5) % 1) % 0.25) // 0.1
        nickel = ((((change % 5) % 1) % 0.25) % 0.1) // 0.05
        penny = (((((change % 5) % 1) % 0.25) % 0.1) % 0.05) // 0.01
        print("Give the customer", d5, "$5 note,", d1, "$1 note,", quarter, "quarter,", dime, "dime,", nickel,"nickel,", penny, "penny.")
        break

2 Comments

also instead of just code dumpping maybe explain yourself on what you did, and what OP did wrong ?
Thank you! This helps a lot !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.