【问题标题】:trying to write a food ordering sys for homework in python试图用python编写一个家庭作业的食品订购系统
【发布时间】:2023-03-04 01:13:02
【问题描述】:

我正在学习 python,为了我的家庭作业,我想制作一个食品订购程序,并获得所购买产品的总价收据。我正在努力获得计费流程,因为我无法将用户选择的所有产品都显示在收据上

import datetime
x = datetime.datetime.now()
name = input("Enter your name: ")
address = input("Enter your address: ")
contact = input("Enter your phone number: ")
print("Hello   " + name)
print("*"*31 + "Welcome, these are the items on on the menu."+"*" * 32 )
print("Menu:\n"
      "1.Burger:150.\n"
      "2.Fried Momo:120.\n"
      "3.coffee:60.\n"
      "4.Pizza:180.\n"
      "5.Fried Rice:150.\n"
      "6.French Fries:90.\n"
      "7.Steamed Dumplings:150.\n"
      "8.Chicken drumsticks:120.\n"
      "9.Chicken Pakoras:120.\n"
      "10.American Chop Suey:200.\n")
prices = {"1.Burger":150,
          "2.Fried Momo":120,
          "3.coffee":60,
          "4.Pizza":180,
          "5.Fried Rice":150,
          "6.French Fries":90,
          "7.Steamed dumplings":150,
          "8.Chicken drumsticks":120,
          "9.Chicken Pakoras":120,
          "10.American Chop Suey":200
}

continue_order = 1
total_cost, total = 0, 0

cart = []
while continue_order != 0:
    option = int(input("Which item would you like to purchase?: "))
    cart.append(option)
    if option >= 10:
        print('we do not serve that here')
        break

    elif option == 1:
        quantity = int(input("Enter the quantity: "))
        total = quantity * 150
        print("The price is: ₹" + str(total))
    elif option == 2:
        quantity = int(input("Enter the quantity: "))
        total = quantity * 120
        print("The price is: " + str(total))
    elif option == 3:
        quantity = int(input("Enter the quantity: "))
        total = quantity * 60
        print("The price is: " + str(total))
    elif option == 4:
        qquantity = int(input("Enter the quantity: "))
        total = quantity * 180
        print("The price is: " + str(total))
    elif option == 5:
        quantity = int(input("Enter the quantity: "))
        total = quantity * 150
        print("The price is: " + str(total))
    elif option == 6:
        quantity = int(input("Enter the quantity: "))
        total = quantity * 90
        print("The price is: " + str(total))
    elif option == 7:
        quantity = int(input("Enter the quantity: "))
        total = quantity * 150
        print("The price is: " + str(total))
    elif option == 8:
        quantity = int(input("Enter the quantity: "))
        total = quantity * 120
        print("The price is: " + str(total))
    elif option == 9:
        quantity = int(input("Enter the quantity: "))
        total = quantity * 120
        print("The price is: " + str(total))
    elif option == 10:
        quantity = int(input("Enter the quantity: "))
        total = quantity * 200
        print("The price is: " + str(total))

    total_cost += total

    continue_order = int(input("Would you like another item? enter Yes--> (1) or--> No (0):"))

print('='*30)
print('='*30)
print("Your receipt:\n")
print("Date: " + str(x))
print("Name: " + name.title())
print("Adress: " + address)
print("Contact number: " + contact)
for option in cart:
    print ("Item: %s. Price: %s") % (option, prices[option])
print("Quantity: ",quantity)
print("Total Price: ", total_cost)
print('='*30)
print("Thank you for shopping here, have a great day ")
print('='*30)

但我在第 95 行遇到错误 print ("Item: %s. Price: %s") % (option, prices[option]) 关键错误:1

任何改进代码的解决方案或更好的方法都会非常棒

【问题讨论】:

    标签: python dictionary


    【解决方案1】:

    尝试使用F-Strings。它们让您更轻松地格式化文本。这是一个例子。

    x = "hello!"
    print(f"shr4pnel says {x}")
    >>> shr4pnel says hello!
    

    在这种情况下的问题是 option == 1. 1 不是字典中的键,因此没有任何输出。希望这可以帮助。这是因为字典没有 1 作为键。要访问该项目,字典必须像这样格式化。

    prices = {1: "burger", 2: "hot dog"}
    print(prices[1])
    >>> burger
    

    【讨论】:

    • 您使用了与原始问题不同的字典。问题在于索引,而不是输出。