简体   繁体   English

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

[英]How to repeat program after user inputs a choice

I am trying to create a text-based purchasing system as an assignment for school. 我正在尝试创建一个基于文本的购买系统,作为学校的作业。 I need help making it so users can "purchase in bulk" meaning after the user chooses a doughnut(one of the menu choices), it would bring back the choices or tell the user to say a keyword and stops. 我需要帮助,以便用户可以选择“甜甜圈”(菜单选项之一)后“批量购买”,这将带回这些选项或告诉用户说出关键字并停止。

Here is an example of how it should go: 这是应该如何进行的示例:

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!

HERE IS MY CODE 这是我的密码

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!")

May I suggest this code skeleton? 我可以建议这个代码框架吗?

...
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)
...

What really matters are the following ideas 真正重要的是以下想法

  • an infinite loop 无限循环
  • if the answer is unsatisfactory, abort and start a new iteration (ie, presenting again the menu) using the continue statement 如果答案不令人满意,请使用continue语句中止并开始新的迭代(即再次显示菜单)
  • if the user chooses to stop interaction, definitively exit the loop using the break statement. 如果用户选择停止交互,则使用break语句最终退出循环。

You could put all your code inside the loop according to these principles, or you can follow my suggestion and abstract repetitive pieces of code into auxiliary functions. 您可以根据这些原则将所有代码放入循环中,也可以按照我的建议, 重复的代码段抽象为辅助函数。

Since this is a homework problem, I don't want to give the code. 由于这是一个家庭作业问题,因此我不想提供代码。 But I will say, you should consider putting all of your code in the while loop and change its terminating condition to 5. (ie, quit when "choice==5") Then you can handle the logic of selections and invalid selections within the loop, say in a series of if-else. 但是,我要说的是,您应该考虑将所有代码放入while循环中,并将其终止条件更改为5。(即,在“ choice == 5”时退出)然后您可以处理选择和无效选择的逻辑循环,在一系列if-else中说。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 选择后重复输入 - Repeat input after choice 用户在python 3.4中输入墙信息后,如何使我的绘画程序继续进行? - how do I get my paint program to contenue after user inputs wall info in python 3.4 如何使用随机重复选择 - How to repeat a choice using random 如何在Python中运行整个程序后重复程序? - How to repeat program after running the whole program in Python? Python:如何在用户输入3个无效值后结束程序,并防止以下功能在不使用系统退出/中断的情况下运行? - Python: How to end a program after user inputs 3 invalid values and prevent following functions from running without using system exit/break? 如何为输入列表重复 function? - How to repeat a function for a list of inputs? 当用户输入 y 时,如何使程序重新运行? - How Can I make the program rerun when the user inputs y? 如何使程序在循环代码处跟踪用户输入? - How do I make the program track user inputs at the loop code? 如何从用户那里获取多个(大量)输入并在用户提供的每个输入中重复代码? (Python) - How can I take multiple (a lot) of inputs from a user and repeat code with each input the user gives? (Python) 如何把选择再试一次重复循环? - How to put a choice to try again to repeat the loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM