Reputation: 143
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!")
Upvotes: 0
Views: 723
Reputation: 25023
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
continue
statementbreak
statement.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.
Upvotes: 2
Reputation: 202
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.
Upvotes: 1