帮我做一个有关银行柜台储蓄业务处理程序的Python代码,具体要求如下:1.输入用户姓名和本金,输出相应的本息和。2.处理至少五笔业务,并将每次的结果保存到文件中。3.能够输出所有业务清单。4.绘制柱形图,呈现用户的本金、利息和本息和,横轴为用户姓名,纵轴为金额
时间: 2024-02-27 22:54:45 浏览: 22
好的,以下是符合要求的Python代码:
```python
import matplotlib.pyplot as plt
def calculate_interest(principal):
interest_rate = 0.03 # 储蓄利率为3%
interest = principal * interest_rate
return interest
def save_record(name, principal, interest, total):
with open('record.txt', 'a') as f:
f.write(f'{name}\t{principal}\t{interest}\t{total}\n')
def read_records():
with open('record.txt', 'r') as f:
print(f'{"姓名":<6} {"本金":<6} {"利息":<6} {"本息和":<6}')
for line in f.readlines():
name, principal, interest, total = line.strip().split('\t')
print(f'{name:<6} {principal:<6} {interest:<6} {total:<6}')
def draw_bar_chart(names, principals, interests, totals):
plt.bar(names, principals, label='本金')
plt.bar(names, interests, bottom=principals, label='利息')
plt.bar(names, totals, bottom=[p+i for p, i in zip(principals, interests)], label='本息和')
plt.xlabel('姓名')
plt.ylabel('金额')
plt.legend()
plt.show()
names = []
principals = []
interests = []
totals = []
for i in range(5):
name = input('请输入姓名:')
principal = float(input('请输入本金:'))
interest = calculate_interest(principal)
total = principal + interest
save_record(name, principal, interest, total)
names.append(name)
principals.append(principal)
interests.append(interest)
totals.append(total)
read_records()
draw_bar_chart(names, principals, interests, totals)
```
运行该程序后,依次输入五笔业务的用户姓名和本金即可。该程序会自动计算每笔业务的利息和本息和,并将结果保存到文件 `record.txt` 中。在处理完五笔业务后,程序会输出所有业务清单,并绘制柱形图,呈现用户的本金、利息和本息和。