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

时间:2018-10-07 21:08:16

标签: python

我正在尝试创建一个基于文本的购买系统,作为学校的作业。我需要帮助,以便用户在选择了“甜甜圈”(菜单选项之一)后可以“批量购买”,这将带回选择或告诉用户说出关键字并停止。

以下是示例说明:

print('Highest grades dict version...')
grades = highest_n_grades_dict(students_dict, 'assignment_2', 2)
print(grades)
print("...and dict structure easily allows us to get other student details")
names_and_grades = [
    (students_dict[id]['first_name'] + ' ' + students_dict[id]['last_name'], grade)
    for id, grade
    in grades]
print(names_and_grades)
>>> python grades.py
Highest grades dict version...
[(12343, 4), (12342, 3)]
...and dict structure easily allows us to get other student details
[('Carl Cape', 4), ('Boris Bank', 3)]

这里是我的密码

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!

2 个答案:

答案 0 :(得分:2)

我可以建议这个代码框架吗?

...
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语句最终退出循环。

您可以根据这些原则将所有代码放入循环中,也可以按照我的建议并将抽象重复的代码段放入辅助函数中。

答案 1 :(得分:1)

由于这是一个家庭作业问题,所以我不想提供代码。 但是我会说,您应该考虑将所有代码放入while循环中,并将其终止条件更改为5。(即,当“ choice == 5”时退出) 然后,您可以处理循环中选择和无效选择的逻辑,例如一系列if-else。