Py学习记录
Python 常规学习
Python学习笔记(一)
Python学习笔记(二)
Python学习笔记(三)
Python学习笔记(四)
Python学习笔记(五)
Python学习笔记(六)
Python学习笔记(七)
Python习题(一)
Python习题(二)
Python习题(三)
Python习题(四)
Python习题(五)
Python常见Bug
Python编程环境
Python-依赖安装(三方库)
Python-VS Code
pip-换源
py 程序转 exe
Python-打开选择文件对话框
Python 项目
Python-密码学
Python-与佛伦禅
Python-喵语翻译
Python-翻译服务器
Python-邮件发送
Python-自动签到
Python-自动签到(Post请求)
Python-自动签到(模拟操作)
Python-图片添加二维码
Python-数据可视化
Python-端口扫描器
Python-未测试项目
Python-虚拟环境
Python-临时环境
Python-venv虚拟环境
Python-Conda
Python-OpenCV
OpenCV-人脸识别
Python-PyTorch
本文档使用 MrDoc 发布
-
+
首页
Python习题(五)
巧算利率,生成Excel 买车需要贷款,贷款的周期按1年、2年、3年、4年、5年进行贷款,不同周期的贷款利率不同,具体利率如下:  车贷贷款在贷款生效后,需要每个月(简称:每期)都进行还款,每月还款的金额包含偿还的本息、偿还的本金、偿还的利息。   第一步:用户只需要将贷款金额与贷款年限输入到程序中,就可以自动计算;此两项数据来源于用户。 第二步:我们的程序要根据用户的贷款金额与贷款年限进行计算,得到每期数据,含偿还的本息、偿还的本金、偿还的利息。 第三步:之后,将所有的数据写入到excel表中。 ## 收集用户输入的内容 ``` total_loan = int(input('请输入贷款总额(贷款总额为整数):')) total_loan_year = int(input('银行贷款基准利率:1年期6.56%;2年期6.65%;3年期6.65%;4年期6.90%;5年期6.90%;请选择还款年限,输入数字即可:') ``` ## 计算数据  每项数据的计算中,都需要【月利率】与【还款月数】 【月利率】之前银行给到过,只需要做分支判断就行了 ### 月利率 > 年利率 ``` #获取用户输入的贷款总额与贷款年限 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 print(year_rate) ``` 在Python中带有百分号的百分数在参与计算时,`不能使用带有百分号的百分数`,要将百分数转换为小数 > 月利率  ``` #月利率 month_rate = year_rate/12 ``` ### 还款月数  ``` #还款月数 loan_month = total_loan_year*12 ``` ### 每月偿还 我们需要计算每月还款的金额包含偿还的本息、偿还的本金、偿还的利息  ``` #循环计算所有月份的数据 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 ``` 使用For循环来计算每个月偿还的 ``` #获取用户输入的贷款总额与贷款年限 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 #循环计算所有月份的数据 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 print(month_money) # 每月还款总额 print(month_capital) # 每月偿还本金 print(month_interest) # 每月偿还利息 ```  ### 累计偿还 每个月的求出来了,我们还需要:累计还款总额、所借本金、累计支付利息 其中所借本金是已知的 累计还款金额 = 每一期还款总额相累加的结果 累计支付的利息 = 累计还款金额-本金 ``` #获取用户输入的贷款总额与贷款年限 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 #循环计算所有月份的数据 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 += month_money print(month_money) # 每月还款总额 print(month_capital) # 每月偿还本金 print(month_interest) # 每月偿还利息 print(sum_money) # 累计还款金额 print(total_loan) # 所借本金 print(sum_money-total_loan) # 累计支付的利息 ```  ## 写入文件 ### 添加表头 ``` import csv #引入csv模块 # from kkb_tools import open_file # 离线不需要下载文件 #调用open()方法,文件名是detaillist.csv,追加模式"a", 文件名在代码中称为listfile,newline='' 是中间不会间隔空行 with open("detaillist.csv","a",newline='',encoding='GBK') as listfile: #使用csv.writer()函数创建writer对象,用于写入 writer = csv.writer(listfile, dialect='excel') #列表头部第一行的字段 header = ['期次','偿还本息(元)','偿还本金(元)','偿还利息(元)'] # 使用writer对象写入表头 writer.writerow(header) # open_file("detaillist.csv") # 离线不需要下载文件 ``` 没有文件会自动创建,有文件则是追加内容 ### 追加每月还款 ``` #获取用户输入的贷款总额与贷款年限 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 import csv #引入csv模块 # from kkb_tools import open_file # 离线不需要下载文件 #调用open()方法,文件名是detaillist.csv,追加模式"a", 文件名在代码中称为listfile,newline='' 是中间不会间隔空行 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 += month_money print(month_money) # 每月还款总额 print(month_capital) # 每月偿还本金 print(month_interest) # 每月偿还利息 # 每个月计算完还款的数据,作为新数据追加到新的一行中 writer.writerow([i, month_money, month_capital, month_interest]) # open_file("detaillist.csv") # 离线不需要下载文件 print(sum_money) # 累计还款金额 print(sum_money-total_loan) # 累计支付的利息 ```  因为是追加,前面表头的时候运行过几次程序,所以会多几个表头 ### 追加合计 ``` #累计数据的表头 total_header = ['总期次', '累计还款总额', '所借本金', '累计支付利息'] # 使用writer对象写入累计数据表头 writer.writerow(total_header) #累计数据 total_data = [loan_month, sum_money, total_loan, sum_money-total_loan] writer.writerow(total_data ``` ``` #获取用户输入的贷款总额与贷款年限 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 import csv #引入csv模块 # from kkb_tools import open_file # 离线不需要下载文件 #调用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 += month_money print(month_money) # 每月还款总额 print(month_capital) # 每月偿还本金 print(month_interest) # 每月偿还利息 # 每个月计算完还款的数据,作为新数据追加到新的一行中 writer.writerow([i, month_money, month_capital, month_interest]) # open_file("detaillist.csv") # 离线不需要下载文件 print(sum_money) # 累计还款金额 print(sum_money-total_loan) # 累计支付的利息 #累计数据的表头 total_header = ['总期次', '累计还款总额', '所借本金', '累计支付利息'] # 使用writer对象写入累计数据表头 writer.writerow(total_header) #累计数据 total_data = [loan_month, sum_money, total_loan, sum_money-total_loan] writer.writerow(total_data) ```  ## 优化显示 1. 在期次后面加上汉字“期”, 形成类似于1期、2期 `str(i)+"期"` 2. 将所有显示金钱的数据,保留2位小数;四舍五入 `round(month_money,2)` ``` #获取用户输入的贷款总额与贷款年限 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 import csv #引入csv模块 # from kkb_tools import open_file # 离线不需要下载文件 #调用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 = round((total_loan * month_rate * (1 + month_rate) ** loan_month) / ( (1 + month_rate) ** loan_month - 1),2) #每月偿还本金 month_capital = round(total_loan * month_rate * ((1 + month_rate) ** (i - 1)) / ((1 + month_rate) ** loan_month - 1),2) #每月偿还利息 month_interest = round(month_money - month_capital,2) # 累计还款金额计算,每月的还款总额都加上 sum_money += month_money print(month_money) # 每月还款总额 print(month_capital) # 每月偿还本金 print(month_interest) # 每月偿还利息 # 每个月计算完还款的数据,作为新数据追加到新的一行中 writer.writerow([str(i)+"期", month_money, month_capital, month_interest]) # open_file("detaillist.csv") # 离线不需要下载文件 print(round(sum_money,2)) # 累计还款金额 print(round(sum_money-total_loan,2)) # 累计支付的利息 #累计数据的表头 total_header = ['总期次', '累计还款总额', '所借本金', '累计支付利息'] # 使用writer对象写入累计数据表头 writer.writerow(total_header) #累计数据 total_data = [loan_month, round(sum_money,2), total_loan, round(sum_money-total_loan,2)] writer.writerow(total_data) ``` 
造物者W
2022年1月11日 12:22
转发文档
收藏文档
上一篇
下一篇
手机扫码
复制链接
手机扫一扫转发分享
复制链接
Markdown文件
分享
链接
类型
密码
更新密码