簡體   English   中英

我如何解決:不能將序列乘以“float”類型的非整數(python)

[英]how i can solve : can't multiply sequence by non-int of type 'float' (python)

我只是 python 的新手,任何人都可以幫我這個代碼,我為這個問題困了一個星期

這是我的代碼:

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")

希望有人能幫我解決這個問題T_T

由於這一行,您收到錯誤消息:

quantity = str(input())
price = 5.80
total = quantity * price

您正在將stringfloat相乘。

TypeError: can't multiply sequence by non-int of type 'float'

謝謝你的幫助,我在這里找到了正確的答案代碼:

menu = "濃縮咖啡,美式咖啡"

打印(菜單)順序=輸入()

#將數量變量更改為 integer 以便它可以用於數學運算? quantity = int(input("你要多少" + order + "?\n"))

#添加一個total變量調用calculate_total function來計算總和

價格根據訂單的數量和價格。

def calculate_total(quantity, price): total = quantity * price 返回總計

total = calculate_total(數量, 價格[訂單])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM