【发布时间】:2021-04-27 06:12:13
【问题描述】:
我需要帮助来完成这段代码。我无法弄清楚如何获得总美元金额。有人能帮忙吗?最后,我希望它有一个总额为 $ 金额的详细收据。我可以让它打印收据,但我可以弄清楚如何获得总数。我最近才开始写代码,所以请不要评判我哈哈。
receipt = ""
print("Pizza size choices: ")
print("1 - small ($5)")
print("2 - medium ($7)")
print("3 - large ($10)")
size_choice = int(input("Your choice: "))
while size_choice < 1 or size_choice > 3:
size_choice = int(input("Not a valid choice: "))
total_cost = 0
if size_choice == 1:
total_cost += 5
receipt += "Size: Small ($5)"
elif size_choice == 2:
total_cost += 7
receipt += "Size: Medium ($7)"
elif size_choice == 3:
total_cost += 10
receipt += "Size: Large ($10)"
receipt += "\n"
print("Pizza crust options: ")
print("1 - thin ($1)")
print("2 - very thin ($1.5)")
print("3 - thick ($2)")
crust_choice = int(input("Your choice: "))
while crust_choice < 1 or crust_choice > 3:
crust_choice = int(input("Not a valid choice: "))
if crust_choice == 1:
total_cost += 1
receipt += "Crust: thin ($1)"
elif crust_choice ==2:
total_cost += 1.5
receipt += "Crust: very thin ($1.5)"
elif crust_choice == 3:
total_cost += 2
receipt += "Crust: thick ($2)"
receipt += "\n"
print("Pizza sauce options: ")
print("1 - Marinara ($0.50")
print("2 - Ranch ($0.75)")
sauce_choice = int(input("Your choice: "))
while sauce_choice < 1 or sauce_choice > 2:
sauce_choice = int(input("Not a valid choice: "))
if sauce_choice == 1:
total_cost += 0.5
receipt += ("Sauce: Marinara ($0.50)")
elif sauce_choice == 2:
total_cost += 0.75
receipt += ("Sauce: Ranch ($0.75)")
receipt += "\n"
print("Cheese options: ")
print("1 - Mozzarella ($0.50")
print("2 - Cheddar ($0.50")
print("3 - Provolone ($0.50)")
cheese_choice = int(input("Your choice: "))
while cheese_choice < 1 or cheese_choice > 3:
cheese_choice = int(input("Not a valid choice: "))
if cheese_choice == 1:
total_cost += 0.5
receipt += ("Cheese: Mozzarella ($0.50)")
elif cheese_choice == 2:
total_cost += 0.5
receipt += ("Cheese: Cheddar ($0.50)")
elif cheese_choice ==3:
total_cost += 0.5
receipt += ("Cheese: Provolone ($0.50)")
receipt += "\n"
print("Topping options (Select up to 3): ")
print("1 - Mushrooms")
print("2 - Peppers")
print("3 - Olives")
print("4 - Onions")
print("5 - Pineapple")
print("6 - Spinach")
topping_choice = int(input("Your choice/choices are: "))
while topping_choice < 1 or topping_choice > 6:
topping_choice = int(input("Not a valid choice: "))
if topping_choice == 1:
receipt += ("Mushrooms")
if topping_choice == 2:
receipt += ("Peppers")
if topping_choice == 3:
receipt += ("Olives")
if topping_choice == 4:
receipt += ("Onions")
if topping_choice == 5:
receipt += ("Pineapple")
if topping_choice == 6:
receipt += ("Spinach")
receipt += "\n"
print(receipt)
【问题讨论】:
-
你不是已经为
total_cost准备了一个变量吗? -
您正在跟踪
total_cost变量中的成本,因此如果您键入print(str(total_cost)),则在脚本末尾(您必须将成本从整数转换为字符串才能打印它) 那么您应该会看到您期望的值。 (请注意,浇头似乎是免费的,虽然提示说“请选择最多三个”,但您似乎无法输入多个。) -
你是对的。关于如何挑选 3 种浇头的任何提示?
标签: python