用Python设计并实现一个银行计算利息的程序。输入为存款金额和存款年限,输出为存款的本利之和,假设年利率为2.1%。存款本利之和=本金+本金*年利率*存款年限。
时间: 2024-09-07 17:03:00 浏览: 99
好的,我们可以使用Python编写一个简单的程序来计算银行存款的本利之和。根据您提供的公式,我们可以设置年利率为2.1%,然后使用输入的存款金额和存款年限来计算最终的本利之和。下面是一个简单的Python程序实现:
```python
# 定义计算本利之和的函数
def calculate_interest(principal, years, rate=2.1):
"""
计算存款的本利之和。
:param principal: 存款金额,float类型
:param years: 存款年限,int类型
:param rate: 年利率,默认为2.1%
:return: 本利之和
"""
interest = principal * rate * years / 100
total_amount = principal + interest
return total_amount
# 从用户获取输入
principal = float(input("请输入存款金额: "))
years = int(input("请输入存款年限: "))
# 调用函数并打印结果
total_amount = calculate_interest(principal, years)
print(f"存款的本利之和为: {total_amount:.2f}")
```
在这个程序中,我们首先定义了一个函数`calculate_interest`,它接受三个参数:`principal`(存款金额),`years`(存款年限),以及一个可选参数`rate`(年利率,默认值为2.1%)。然后,我们通过简单的数学运算计算出利息并累加到本金上得到本利之和。
用户通过`input`函数输入存款金额和存款年限,输入的金额会被转换为浮点数,年限会被转换为整数。之后,调用`calculate_interest`函数计算本利之和,并打印出来。
阅读全文
相关推荐
![](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)
![](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)
![](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)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pptx](https://img-home.csdnimg.cn/images/20241231044947.png)