<p>这里有一些问题。你知道吗</p>
<p>第一个是响应选择的逻辑在<code>while</code>循环之外。可以通过缩进整块来固定。你知道吗</p>
<p>第二,当用户输入<code>5</code>时,<code>while choice not in [1,2,3,4]:</code>中的条件计算为<code>True</code>,因此提示用户再次输入有效的选项。这可以通过完全移除内部<code>while</code>循环来解决。你知道吗</p>
<p>最后,在到达<code>elif choice == 5</code>块时,用户将看不到这些收据打印中的任何一个,因为<code>choice</code>是<code>5</code>,因此不是<code>1</code>、<code>2</code>、<code>3</code>或<code>4</code>。我想你这里的意思是<code>chocolate</code>、<code>strawberry</code>、<code>vanilla</code>或<code>honey</code>的计数为非零。而且这些都应该是<code>if</code>而不是<code>elif</code>块,因为它们相互独立(用户可以得到一些巧克力和香草)。你知道吗</p>
<p>考虑到所有这些,这里是一个重构:</p>
<pre><code>print("Welcome to Dino's International Doughnut Shoppe!")
name = input("Please enter your name to begin: ")
#doughnuts menu
chocolate = strawberry = vanilla = honey = 0
done = False
while not done:
print("Please enter a valid choice from 1-4.")
print("Please select a doughnut from the following menu: ")
print("1. Chocolate-dipped Maple Puff ($3.50 each)")
print("2. Strawberry Twizzler ($2.25 each)")
print("3. Vanilla Chai Strudel ($4.05 each)")
print("4. Honey-drizzled Lemon Dutchie ($1.99)")
print("5. No more doughnuts.")
choice = int(input(">"))
if choice == 1:
chocolate = int(input("How many chocolate-dipped Maple Puff(s) would you like to purchase? "))
elif choice == 2:
strawberry = int(input("How many Strawberry Twizzler(s) would you like to purchase? "))
elif choice == 3:
vanilla = int(input("How many Vanilla Chai Strudel(s) would you like to purchase? "))
elif choice == 4:
honey = int(input("How many Honey-drizzled Lemon Dutchie(s) would you like to purchase? "))
elif choice == 5:
done = True
print(f"{name}, Here is your receipt: ")
if chocolate > 1:
print("==========================================")
print(f"{chocolate} Chocolate Dipped Maple Puffs")
print("==========================================")
print(f"Total Cost: ${chocolate*3.50:.2f}")
if strawberry > 1:
print("==========================================")
print(f"{strawberry} Strawberry Twizzlers")
print("==========================================")
print(f"Total Cost: ${strawberry*2.25:.2f}")
if vanilla > 1:
print("==========================================")
print(f"{vanilla} Vanilla Chai Strudels")
print("==========================================")
print(f"Total Cost: ${vanilla*4.05:.2f}")
if honey > 1:
print("==========================================")
print(f"{honey} Honey-drizzled Lemon Dutchies")
print("==========================================")
print(f"Total Cost: ${honey*1.99:.2f}")
print("Thank you for shopping at Dino's International Doughnut Shoppe! Please come again!")
</code></pre>