【发布时间】:2020-06-02 20:21:15
【问题描述】:
我正在尝试在一个程序中使用两个函数制作货币转换器,但由于某种原因,我的程序要么出现此错误:
line 23, in Input1
print(amount+"Won equals to "+totalamount+" USD")
TypeError: unsupported operand type(s) for +: 'float' and 'str'
或者在输入“选项”和“金额”后不打印任何内容。这是我的程序:
print("Please choose which currency you want to convert:")
print("A - Korean Won to US Dollar (Exchange Rate: 0.000905)")
print("B - Korean Won to Euro (Exchange Rate: 0.000807350908)")
print("C - Korean Won to Japanese Yen (Exchange Rate: 0.0919061643)")
print("D - Korean Won to Chinese RMB (Exchange Rate: 0.00603703605)")
print("E - Quit")
usd = 0.000905
eur = 0.000807350908
yen = 0.0919061643
rmb = 0.00603703605
def main():
option =input("Enter your option: ")
if option== "E":
exit()
amount =float(input("Enter the amoutn in Korean Won: "))
Input1(option, amount)
def Input1(option,amount):
if option == "A":
totalamount = (amount * usd)
print(amount+"Won equals to "+totalamount+" USD")
elif option== "B":
totalamount = (amount * eur)
print (amount+"Won equals to "+totalamount+" Euro")
elif option== "C":
totalamount = (amount * yen)
print (amount+"Won equals to "+totalamount+" Yen")
elif option== "D":
totalamount = (amount * rmb)
print (amount+"Won equals to "+totalamount+" Chinese RMB")
else:
print("You entered an invalid input.")
return option,amount
main()
你们能给我一些提示来修复我的程序吗?我仍在学习 python,任何帮助将不胜感激。太感谢了!
【问题讨论】:
-
嘿@papara,一般来说,如果你能删掉不必要的代码并且只提供一个最小的可重现的问题示例,StackOverflow 社区会很感激。例如,在您的情况下,这样一个最小的示例可能是
print(123+"Won equals to "+456+" USD")这表明了相同的错误,我们可以更快地为您提供帮助。 -
您也应该在此处发布之前搜索错误消息。如果您搜索
python unsupported operand type(s) for +: 'float' and 'str',您会从 Stackoverflow 中获得很多回答相同问题的结果,例如 stackoverflow.com/questions/19480060/… 或 stackoverflow.com/questions/22664491/… 或 stackoverflow.com/questions/43106154/…