【问题标题】:Currency convertor using two functions使用两个功能的货币转换器
【发布时间】: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/…

标签: python typeerror currency


【解决方案1】:

您只需要将当前浮动的金额和总金额变量类型转换为字符串

print(str(amount)+"Won equals to "+str(totalamount)+" USD")

或者使用你使用格式功能

print("{} Won equals to {} USD ".format(amount, totalamount))

我认为格式功能是更好的选择

【讨论】:

    【解决方案2】:

    您必须先将amounttotalamount 转换为字符串,然后才能在print() 中将其用作带+ 的字符串。您可以使用str() 方法。

    print(str(amount)+"Won equals to "+str(totalamount)+" USD")
    

    【讨论】:

      【解决方案3】:

      使用 f 字符串代替字符串连接;它会为您处理转换!

      print(f"{amount} won equals to {totalamount} USD")
      

      如果您确实想自己进行转换,则如下所示:

      print(str(amount) + "Won equals to " + str(totalamount) + " USD")
      

      (编辑)回答 cmets 中提出的问题,将您的 main() 函数更改为:

      def main():
          while True:
              option = input("Enter your option: ")
              if option== "E":
                  break
              amount = float(input("Enter the amount in Korean Won: "))
              Input1(option, amount)
      

      【讨论】:

      • 谢谢!这解决了我的问题。还有一个问题..我怎样才能让它在打印出转换结果后重新启动程序?
      猜你喜欢
      • 2021-09-03
      • 1970-01-01
      • 2012-01-22
      • 1970-01-01
      • 2014-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多