请在python的一个函数里实现存款功能:可按不同货币存款,例如人民币、美元、欧元;并将美元,欧元转化为人民币,显示余额为人民币,输出余额
时间: 2024-03-31 14:34:37 浏览: 20
好的,可以使用一个字典来存储不同货币的汇率,然后在存款时进行转换并更新余额。以下是一个实现示例:
```python
exchange_rate = {'USD': 6.5, 'EUR': 7.8} # 汇率表,以人民币为基准
balance = {'CNY': 0, 'USD': 0, 'EUR': 0} # 存储各种货币的余额
def deposit(amount, currency):
if currency == 'CNY':
balance[currency] += amount
elif currency in exchange_rate:
cny_amount = amount * exchange_rate[currency]
balance['CNY'] += cny_amount
balance[currency] += amount
else:
print('Unsupported currency:', currency)
print('Balance:', balance['CNY'], 'CNY')
```
使用示例:
```python
deposit(100, 'CNY')
# Output: Balance: 100 CNY
deposit(50, 'USD')
# Output: Balance: 425 CNY
deposit(20, 'EUR')
# Output: Balance: 585 CNY
deposit(10, 'JPY')
# Output: Unsupported currency: JPY
# Output: Balance: 585 CNY
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)