import csv #引入csv模块
import math
#获取用户输入的贷款总额与贷款年限
total_loan = int(input('请输入贷款总额(贷款总额为整数):'))
total_loan_year = int(input('银行贷款基准利率:1年期6.56%;2年期6.65%;3年期6.65%;4年期6.90%;5年期6.90%;请选择还款年限,输入数字即可:'))
#年利率
year_rate = 0
if total_loan_year==1: #1年期
year_rate = 0.0656
elif 1<total_loan_year<=3: #2年期、3年期
year_rate = 0.0665
elif 3<total_loan_year<=5: #4年期、5年期
year_rate = 0.069
#月利率
month_rate = year_rate/12
#还款月数
loan_month = total_loan_year*12
#累计还款总额
sum_money = 0
#调用open()方法,文件名是detaillist.csv,追加模式"a", 文件名在代码中称为listfile
with open("detaillist.csv","a",newline='',encoding='GBK') as listfile:
#使用csv.writer()函数创建writer对象,用于写入
writer = csv.writer(listfile, dialect='excel')
#列表头部第一行的字段
header = ['期次','偿还本息(元)','偿还本金(元)','偿还利息(元)']
# 使用writer对象写入表头
writer.writerow(header)
#循环计算所有月份的数据
for i in range(1, loan_month + 1):
print("第" + str(i) + "月还款情况")
#每月还款总额
month_money = (total_loan * month_rate * (1 + month_rate) ** loan_month) / (
(1 + month_rate) ** loan_month - 1)
#每月偿还本金
month_capital = total_loan * month_rate * ((1 + month_rate) ** (i - 1)) / ((1 + month_rate) ** loan_month - 1)
#每月偿还利息
month_interest = month_money - month_capital
#累计还款总额计算
sum_money = sum_money+month_money
print(month_money)
print(month_capital)
print(month_interest)
writer.writerow(['%d期'%i,round(month_money,2) ,round(month_capital,2) ,round(month_interest,2)])
#累计利息
sum_interest = sum_money - total_loan
#累计字段
total_header = ['总期次', '累计还款总额', '所借本金', '累计支付利息']
# 使用writer对象写入表头
writer.writerow(total_header)
#累计数据
total_data = ['%d期次'%loan_month,round(sum_money,2) ,round(total_loan,2) ,round(sum_interest,2) ]
writer.writerow(total_data)
print(sum_money)
print(total_loan)
print(sum_interest)
print('打印完毕')
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
python之Excel表格处理.zip (14个子文件)
Excel表格处理
用excel储存豆瓣电影.py 2KB
写入csv文件.jpg 195KB
excel的读取.py 206B
score.xlsx 5KB
读取excel文件.jpg 256KB
计算本息.py 2KB
写入excel文件.jpg 241KB
读取csv文件.jpg 187KB
csv的写入和读取.py 663B
用excel存储五月天歌单.py 2KB
excel的写入.py 770B
movie.xlsx 7KB
mayday.xlsx 8KB
detaillist.csv 1KB
共 14 条
- 1
资源评论
发光发热小流星
- 粉丝: 10
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功