[英]how i can solve : can't multiply sequence by non-int of type 'float' (python)
i'm just newbie for python, can anyone help me this code, im stuck for a week for this problem我只是 python 的新手,任何人都可以帮我这个代码,我为这个问题困了一个星期
here my code:这是我的代码:
price = {
"Espresso": 5.80,
"Americano": 6.90,
}
currency = "$"
print ("welcome coffee machine!\t")
name = input ("can i get your name?\n")
print ("what do you like to order mr/ms " + name + "\n" )
menu = ("Espresso, Americano")
print (menu)
menu = (input())
quantity = input("How many " + menu + " would you like?\n")
quantity = str(input())
#im stuck at here! T_T
if menu == "Espresso" :
price = 5.80
total = quantity * price
quantity.append(quantity)
price.append(price)
print(total)
elif menu == "Americano":
price = 6.90
total = quantity * price
quantity.append(quantity)
price.append(price)
print(total)
else:
menu()
#invoice receipt
print("Thank you for order " + name + ", please wait your " + menu + " at counter\n")
hopefully someone/somebody can help me to solve this problem T_T希望有人能帮我解决这个问题T_T
You are getting error because of this line:由于这一行,您收到错误消息:
quantity = str(input())
price = 5.80
total = quantity * price
You are multiplying string
with float
.您正在将
string
与float
相乘。
TypeError: can't multiply sequence by non-int of type 'float'
thank you for you help, i found my answer here the correct code:谢谢你的帮助,我在这里找到了正确的答案代码:
menu = "Espresso, Americano" menu = "浓缩咖啡,美式咖啡"
print (menu) order = input()打印(菜单)顺序=输入()
#changed the quantity variable to an integer so that it can be used in mathematical operations? #将数量变量更改为 integer 以便它可以用于数学运算? quantity = int(input("How many " + order + " would you like?\n"))
quantity = int(input("你要多少" + order + "?\n"))
#added a total variable and called the calculate_total function to calculate the total #添加一个total变量调用calculate_total function来计算总和
def calculate_total(quantity, price): total = quantity * price return total def calculate_total(quantity, price): total = quantity * price 返回总计
total = calculate_total(quantity, price[order]) total = calculate_total(数量, 价格[订单])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.