【问题标题】:Trying to run a Coffee shop code in python, isn't working尝试在 python 中运行咖啡店代码,但无法正常工作
【发布时间】:2023-02-22 09:29:05
【问题描述】:
prices=[120,130,120,150]

menu = {
    1: ["Latte", 120],
    2: ["Capputino", 130],
    3: ["Americano", 120],
    4: ["Espresso",150]
}

headers = ["No.", 'Name', "Price"]
print(f'{headers[0]: <10}{headers[1]: <15}{headers[2]}')

for value, key in menu.items():
    print(f'{value: <10}{key[0]: <15}{key[1]}')

myorder_coffee=[]
myorder_cost=[]
total=0

print("Welcome to COFFEE & DELIGHT")

name=input("Enter your name: ")
order=input("Enter the name of your order: ")
quantity=input("Enter the amount of drinks.you want to order: ")

if order=="Latte":
    myorder_coffee.append(coffee[0])
    myorder_cost.append(prices[0])
    total=quantity*120
    print(name,"ordered",quantity,"Latte")
    print("Total amount to be paid=",total)
elif order=="Capputino":
    myorder_coffee.append(coffee[1])
    myorder_cost.append(prices[1])
    total=quantity*130
    print(name,"ordered",quantity,"Capputino")
    print("Total amount to be paid=",total)
elif order=="Americano":
    myorder_coffee.append(coffee[2])
    myorder_cost.append(prices[2])
    total=quantity*120
    print(name,"ordered",quantity,"Americano")
    print("Total amount to be paid=",total)
elif order=="Espresso":
    myorder_coffee.append(coffee[3])
    myorder_cost.append(prices[3])
    total=quantity*150
    print(name,"ordered",quantity,"Espresso")
    print("Total amount to be paid=",total)
else:
    print("Item not in menu")

好吧,我完全没想到会发生这种情况。

No.       Name           Price
1         Latte          120
2         Capputino      130
3         Americano      120
4         Espresso       150
Welcome to COFFEE & DELIGHT
Enter your name: pp
Enter the name of your order: Americano
Enter the amount of drinks. you want to order: 4
pp ordered 4 Americano
Total amount to be paid= 444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444

[Program finished]]

Link to screengrab of above program run.

【问题讨论】:

  • 请查看how to ask page以及write a MRE。当前帖子格式不正确并且缺少一些变量声明。
  • 您忘记将 quantity 转换为整数,而是一个字符串。实际上将 total 变成了 120 '4'

标签: python dictionary


【解决方案1】:
quantity=input("Enter the amount of drinks.you want to order: ")

input() 返回一个字符串。所以quantity 是一个字符串。

total=quantity*120

当您将一个字符串乘以一个整数时,您会得到该字符串的重复副本。所以你得到了字符串“4”,重复了 120 次。

如果你想要整数输入,使用int()

quantity=int(input("Enter the amount of drinks.you want to order: "))

【讨论】:

    猜你喜欢
    • 2017-01-07
    • 2012-02-08
    • 2014-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多