用python写一个汇率计算器
时间: 2023-05-14 11:06:09 浏览: 203
python 实现一个图形界面的汇率计算器
好的,以下是一个简单的汇率计算器的 Python 代码:
```python
import requests
def exchange_rate(from_currency, to_currency, amount):
url = f"https://api.exchangerate-api.com/v4/latest/{from_currency}"
response = requests.get(url)
data = response.json()
exchange_rate = data["rates"][to_currency]
converted_amount = amount * exchange_rate
return converted_amount
from_currency = input("请输入原始货币代码:")
to_currency = input("请输入目标货币代码:")
amount = float(input("请输入要兑换的金额:"))
result = exchange_rate(from_currency, to_currency, amount)
print(f"{amount} {from_currency} = {result} {to_currency}")
```
这个程序使用了 exchangerate-api.com 的 API 来获取汇率信息,然后根据用户输入的原始货币代码、目标货币代码和金额来计算兑换后的金额。
阅读全文