繁体   English   中英

用户输入选择后如何重复程序

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM