如何确保 while 循环不会循环到先前的输入?

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

我的程序很简单,尽管它很长。它只是一个购物程序,在购买食物对象后显示用户的预算。我是 Python 的新手,所以我的代码很乱,需要大量工作。

我的问题是我的程序一直循环回到之前的输入而不是刚刚输入的输入。一个例子是我去食品区买了一个食品对象,然后我离开了食品所在的部分产品是。但是,当我尝试通过键入“Nothing”离开我的程序时,它会返回到我上次输入的部分。这里有一些上下文图像。

Starting the program by inputting a budget

Going to a food section

Leaving the food section

The wrong loop (the main problem)

这是程序。实在是乱七八糟,不好意思看。

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: .00\n2. $Candy Bag: .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()
items = input("Do you want dairy, meat, vegetables, fruits, or desserts! Type 
‘Nothing’ if you don’t want to buy anything...")
     while True:

将 items = input() 行移至 while true: 循环下方,以便在每次循环迭代时询问它。目前你只问一次,然后每次循环开始时使用那个答案作为变量,这就是为什么在这种情况下它总是会回到沙漠。