正如您所看到的,我正在尝试制作货币转换器。我想要做的是说如果输入数字“1”,那么转到函数'gbp'。它工作正常,直到它询问你想要转换多少钱(第25行)。当我输入一个值然后按Enter键时会出现一个回溯错误,告诉我所有第33,5和29行都有“不支持的操作数类型+:''float'和'str'”。 / p>
这是我的代码:
def start():
print("Hello and welcome to the very limited currency converter!\nIn this converter, the number '1' means Pounds Sterling, number '2' means US Dollars, '3' means Euros and '4' means Japanese Yen.")
From = input("Which currency do you wish to convert from?\n")
if From == ("1"):
gbp()
return
elif From == ("2"):
usd()
return
elif From == ("3"):
euro()
return
elif From == ("4"):
yen()
return
else:
print("That is not a valid input. Please restart the program and input either '1', '2', '3' or '4'!")
return start
def gbp():
gbpTo = input("Which currency are you converting into?\n")
if gbpTo == ("1"):
print("You are converting Pounds Sterling to Pounds Sterling... there is no conversion needed!")
elif gbpTo == "2":
num = float(input("Please type in the amount of Pounds Sterling that you wish to convert into US Dollars.\n"))
calc = num * 1.55
float(calc)
calc = round(calc, 2)
print(num + " Pounds Sterling in US Dollars is $", + calc)
return
start()
我很感激任何帮助。感谢
答案 0 :(得分:1)
您正在尝试添加浮点数和字符串。 Python不知道您想要执行哪种添加,因此您需要在将num
和calc
连接到字符串之前将其转换为字符串:
print(str(num) + " Pounds Sterling in US Dollars is $" + str(calc))
答案 1 :(得分:0)
而不是
print(num + " Pounds Sterling in US Dollars is $", + calc)
DO
print("%f Pounds Sterling in US Dollars is $ %f" % (num, calc))