用python写一个美元,英镑,日元等外币与人民币汇转换的计算器率
时间: 2024-06-11 17:05:48 浏览: 95
以下是一个简单的汇率转换计算器,可以将美元、英镑、日元和欧元转换为人民币:
```python
def currency_converter(amount, currency):
"""Converts foreign currency to CNY based on current exchange rates"""
if currency == "USD":
rate = 6.46 # current USD to CNY exchange rate
result = amount * rate
return result
elif currency == "GBP":
rate = 8.96 # current GBP to CNY exchange rate
result = amount * rate
return result
elif currency == "JPY":
rate = 0.059 # current JPY to CNY exchange rate
result = amount * rate
return result
elif currency == "EUR":
rate = 7.66 # current EUR to CNY exchange rate
result = amount * rate
return result
else:
print("Invalid currency type")
# Example usage:
print(currency_converter(100, "USD")) # Output: 646.0
print(currency_converter(50, "GBP")) # Output: 448.0
print(currency_converter(5000, "JPY")) # Output: 295.0
print(currency_converter(200, "EUR")) # Output: 1532.0
```
请注意,这个计算器的汇率是基于当前市场汇率,而这些汇率会随着时间和市场变化而变化。因此,此代码可能需要定期更新以反映最新的汇率。
阅读全文