Home > database >  How do you make sure that a while loop doesn't loop to a previous input?
How do you make sure that a while loop doesn't loop to a previous input?

Time:09-25

My program is simple, even though it is long. It is just a shopping program that displays the user's budget after buying a food object. I am relatively new to Python so my code is pretty messy and needs a lot of work.

My problem is that my program keeps looping back to a previous input instead of the input just put in. An example is I go to a food section and buy a food object, then I leave the section in which the food product is. But when I try to leave my program by typing "Nothing," it goes back to the section which I last inputted. Here are some images for context.

Starting the program by inputting a budget

Going to a food section

Leaving the food section

The wrong loop (the main problem)

Here is the program. Its really messy so I am sorry if it is hard to read.

x = 50 #budget
y = 5 #small budget
#Item Prices

#desserts
candy_bar = 1
candy_bag = 2
ice_cream = 2

#Welcome to the store
print("Hello there, young laddie, young man, young boy! Welcome to the Normal Big Meal Grocery Store, where our store is your store! ;)")

#How much money do you want to spend today
budget = int(input("What is your budget today?")) # at the moment, i do not have a shopping cart
if budget <= y:
    print("You literally can only buy like 5 candy bars. You don't have enough money! Go to the clearance store across the street!")
    print("**You leave and go to the clearance store...")
    exit()  #you leave
elif budget <= x:
    print("I guess you can buy something...But it's not enough for a big meal! You might need to go to the Normal Medium Meal Grocery Store!")
    print("**You leave and go to the Normal Medium Meal Grocery Store...")
    exit() #you leave
elif budget >= x:
    print("Cool! We have lot of stuff for you to make into a big meal, perhaps even a giant one!!!")
    print("**Takes you by the hand and leads you into the beautiful Normal Big Meal Grocery Store...")

#User input for food:
items = input("Do you want dairy, meat, vegetables, fruits, or desserts! Type ‘Nothing’ if you don’t want to buy anything...")
while True:
    def all_items():
        items = input("Do you want dairy, meat, vegetables, fruits, or desserts! Type ‘Nothing’ if you don’t want to buy anything...")
    if items == ("Nothing"):
        bye = input("Are you sure you don't want to buy anything? Yes or No?")
        if bye == ("Yes"):
            print("Thank you for shopping at the Normal Big Meal Grocery Store!!!")
            exit()
        if bye == ("No"):
            all_items()
    elif items == ("desserts"):
        print("Here are our desserts!\n1. Candy Bar: $1.00\n2. $Candy Bag: $2.00\n3. $Ice Cream: 2.00")
        dessert_items = input("What desserts do you want to buy? Type 'Nothing' if you don't want any...")
        if dessert_items == ("Candy Bar"):
            print("Are you sure you want to buy this?")
            candybarQ = input("Yes or no?")
            if candybarQ == ("Yes"):
                print("You have $",(budget - candy_bar), "left!")
            if candybarQ == ("No"):
                all_items()
        if dessert_items == ("Candy Bag"):
            print("Are you sure you want to buy this?")
            candybagQ = input("Yes or no?")
            if candybagQ == ("Yes"):
                print("You have $",(budget - candy_bag), "left!")
            if candybagQ == ("No"):
                all_items()
        if dessert_items == ("Ice Cream"):
            print("Are you sure you want to buy this?")
            icecreamQ = input("Yes or no?")
            if icecreamQ == ("Yes"):
                print("You have $",(budget - ice_cream), "left!")
            if icecreamQ == ("No"):
                all_items()
        if dessert_items == ("Nothing"):
            print("Are you sure you don't want any?")
            nothingQ = input("Yes or no?")
            if nothingQ == ("Yes"):
                all_items()
            if nothingQ == ("No"):
                continue
    else:
        print("Umm. We don't have that...")
        all_items()

CodePudding user response:

    items = input("Do you want dairy, meat, vegetables, fruits, or desserts! Type 
    ‘Nothing’ if you don’t want to buy anything...")
         while True:

Move the items = input() line below the while true: loop so that it asks it every time the loop iterates. Currently you're only asking once and then using that answer as the variable each time the loop starts which is why in this case it will always go back to desert.

  • Related