python - 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)
解决方案
可能是因为您没有使用打印功能
您可以使用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())
显示最终结果
推荐阅读
- html - 搜索容器不显示
- python - 遍历列表并将列转换为数据时间
- amazon-web-services - 从 AWS lambda 向 IOT 设备发送请求并等待响应
- context-free-grammar - 上下文无关和上下文敏感的语法是什么意思?
- r - 在ggplot2中对齐多线轴标题
- vba - 如何防止一个段落跨越两页
- d3.js - vega-lite:如何将 JSON 中的日期指定为包含年、月和日数的单独字段?
- javascript - 点击被忽略:标签元素与表单控件相关联的问题
- java - 在仍然更改属性的同时重试 Spring Data Flow 任务
- ruby-on-rails - 覆盖模型中的 ActiveSupport::Concern