[英]How to repeat program after user inputs a choice
我正在嘗試創建一個基於文本的購買系統,作為學校的作業。 我需要幫助,以便用戶可以選擇“甜甜圈”(菜單選項之一)后“批量購買”,這將帶回這些選項或告訴用戶說出關鍵字並停止。
這是應該如何進行的示例:
Welcome, to Dino's International Doughnut Shoppe!
Please enter your name to begin: Andrew
Please select a doughnut from the following menu:
1. Chocolate-dipped Maple Puff ($3.50 each)
2. Strawberry Twizzler ($2.25 each)
3. Vanilla Chai Strudel ($4.05 each)
4. Honey-drizzled Lemon Dutchie ($1.99)
5. No more doughnuts.
> 7
I'm sorry, that's not a valid selection. Please enter a selection from 1-5.
Please select a doughnut from the following menu:
1. Chocolate-dipped Maple Puff ($3.50 each)
2. Strawberry Twizzler ($2.25 each)
3. Vanilla Chai Strudel ($4.05 each)
4. Honey-drizzled Lemon Dutchie ($1.99)
5. No more doughnuts.
> 1
How many Chocolate-dipped Maple Puffs would you like to purchase? 12
Please select a doughnut from the following menu:
1. Chocolate-dipped Maple Puff ($3.50 each)
2. Strawberry Twizzler ($2.25 each)
3. Vanilla Chai Strudel ($4.05 each)
4. Honey-drizzled Lemon Dutchie ($1.99)
5. No more doughnuts.
> 4
How many Honey-drizzled Lemon Dutchies would you like to purchase? 8
Please select a doughnut from the following menu:
1. Chocolate-dipped Maple Puff ($3.50 each)
2. Strawberry Twizzler ($2.25 each)
3. Vanilla Chai Strudel ($4.05 each)
4. Honey-drizzled Lemon Dutchie ($1.99)
5. No more doughnuts.
> 4
How many Honey-drizzled Lemon Dutchies would you like to purchase? 3
Please select a doughnut from the following menu:
1. Chocolate-dipped Maple Puff ($3.50 each)
2. Strawberry Twizzler ($2.25 each)
3. Vanilla Chai Strudel ($4.05 each)
4. Honey-drizzled Lemon Dutchie ($1.99)
5. No more doughnuts.
> 5
Andrew, here is your receipt:
-------------------------------------
12 Chocolate-dipped Maple Puffs
3 Honey-drizzled Lemon Dutchies
-------------------------------------
Total cost: $47.97
Thank you, have a nice day!
這是我的密碼
print("Welcome to Dino's International Doughnut Shoppe!")
name = input("Please enter your name to begin: ")
choice = 0
while choice not in [1,2,3,4]:
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? "))
print(f"{name}, Here is your receipt: ")
if choice == 1:
print("==========================================")
print(f"{chocolate} Chocolate Dipped Maple Puffs")
print("==========================================")
print(f"Total Cost: ${chocolate*3.50:.2f}")
elif choice == 2:
print("==========================================")
print(f"{strawberry} Strawberry Twizzlers")
print("==========================================")
print(f"Total Cost: ${strawberry*2.25:.2f}")
elif choice == 3:
print("==========================================")
print(f"{vanilla} Vanilla Chai Strudels")
print("==========================================")
print(f"Total Cost: ${vanilla*4.05:.2f}")
elif choice == 4:
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!")
我可以建議這個代碼框架嗎?
...
while True: # i.e., loop F O R E V E R
reply = present_menu()
if not (1 <= reply <= 5) :
show_error_message()
continue # i.e., abort this cycle and start a new loop cycle
if reply == 5:
recapitulation()
break # i.e., exit the forever loop
how_many = ask_how_many(reply)
update_purchases(reply, how_many)
...
真正重要的是以下想法
continue
語句中止並開始新的迭代(即再次顯示菜單) break
語句最終退出循環。 您可以根據這些原則將所有代碼放入循環中,也可以按照我的建議, 將重復的代碼段抽象為輔助函數。
由於這是一個家庭作業問題,因此我不想提供代碼。 但是,我要說的是,您應該考慮將所有代碼放入while循環中,並將其終止條件更改為5。(即,在“ choice == 5”時退出)然后您可以處理選擇和無效選擇的邏輯循環,在一系列if-else中說。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.