不能将序列乘以'float'Python类型的非整数

时间:2019-09-16 19:29:15

标签: python

所以我目前正在尝试进行货币换算。这是代码:

Choice = 0
Amount = 0
Converted = 0
Staff = False
print("Welcome to currency converter - Here are your options: ")
print("1. Dollars to sterling")
print("2. Euros to sterling")
print("3. Sterling to dollars")
print("4. Sterling to euros")
Choice=input("Please type the number of your choice")
Amount=input("Enter the amount you would like to convert")
if Choice == "1":
    Amount = Amount * 0.80
elif Choice == "2":
    Amount = Amount * 0.89
print(Amount)

这是我得到的错误:

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

我尝试了以下解决方案:

Amount=input(float("Enter the amount you would like to convert"))
if Choice == "1":
    Amount = Amount * float(0.80)
if Choice == "1":
    Amount = float(Amount * 0.80)

这些解决方案均无法正常工作,而且我一直收到相同的错误-在提供修复程序时,您会以一种基本的方式解释为什么会发生此错误,我将不胜感激-谢谢!

1 个答案:

答案 0 :(得分:2)

Amountstr,而不是float。您需要先进行明确的转换。

Amount = float(input("..."))

Amount = float(Amount) * 0.80

您正在将float(...)应用于几乎所有内容,除了需要应用的内容之外。