首页 > 解决方案 > Python中的货币转换器

问题描述

我正在学习 python,我的任务是编写一个转换货币的程序。它需要支持这些货币:BGN、USD、EUR、GBP,汇率如下:

汇率 USD EUR GBP 1 BGN 1.79549 1.95583 2.53405

有自动测试引擎,将输入值转换+输入货币+目标货币。输出应该是一个由费率转换的数字。

我以为我可以使用字典并执行以下代码之类的操作。你会帮助找出它为什么不起作用并找到优雅的解决方案吗?

value = float(input())
in_curr = input()
out_curr = input() 

dict = {'BGN': 1, 'USD': 1.79549, 'EUR': 1.95583, 'GBP': 2.53405}

def currency_converter (value,in_curr,out_curr):
    return((dict[in_curr] / dict[out_curr]) * value)  

标签: python

解决方案


可能是因为您没有使用打印功能
您可以使用IPython(即Jupyter Notebook)直接查看结果

您的功能看起来不错,我建议您删除该功能

print((dict[in_curr] / dict[out_curr]) * value) #python 3

或将函数更改为:

def currency_converter (value = value,in_curr = in_curr,out_curr = out_curr):

然后用于print(currency_converter())显示最终结果


推荐阅读