【问题标题】:Having trouble finishing this python code无法完成此 python 代码
【发布时间】: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


【解决方案1】:

在脚本结束时,因为您已经在跟踪总成本,所以只需打印变量 total_cost。 #注意:这应该放在脚本的末尾

print(receipt)
print(f'total cost {total_cost}')

编辑 出于我内心的善意,我已经创建了一个基本概念,即您可以做些什么来实现提供三个顶级选择的目标。 下面的代码要求用户提供一些顶级选择。然后问他们想要什么配料。这是基本的,我希望你 OP 把它清理干净并放在受人尊敬的地方。

chosenToppings = []
while True:
    toppingCount = int(input('How many toppings would you like?: '))
    if toppingCount <= 3:
        for i in range(1,toppingCount+1):
            top = 1
            while top in [1,2,3]:
                top = int(input('Enter choice 1-3: '))
                chosenToppings.append(top)
                break
        break
    else:print('Enter a number less than three please')

【讨论】:

  • 非常感谢。还有一个问题:用户应该最多可以选择 3 个浇头,当我在代码中输入 3 个数字时,它会给我一个错误提示:浇头选项(最多选择 3 个):1 - 蘑菇 2 - 辣椒 3 -橄榄 4 - 洋葱 5 - 菠萝 6 - 菠菜 您的选择/选择是:1 2 3 Traceback(最近一次调用最后一次):文件“C:\Users\spman\OneDrive\Documents\pizzapt2.py”,第 98 行,在 topping_choice = int(input("Your choice/choices are: ")) ValueError: invalid literal for int() with base 10: '1 2 3' 我该如何解决这个问题?
  • 是的,因为你不能把 1 2 3 变成一个整数。您需要在空间处拆分它,遍历该列表并将每个数字转换为一个 int。
【解决方案2】:

如果您只想打印总成本而不是仅在最后打印总成本

【讨论】:

    猜你喜欢
    • 2020-03-27
    • 2011-04-20
    • 1970-01-01
    • 2019-05-20
    • 2021-12-09
    • 2011-10-01
    • 2011-10-12
    • 2012-12-23
    • 1970-01-01
    相关资源
    最近更新 更多