89. 四则运算练习题及Python等额本息代码
1. 加法运算
【目标任务】
张三原持有股票数量为1000,今天张三又买入500股,用input
函数从终端输入的方式输出张三现在持有的股数。
【输入】
张三原持有股数:1000
张三今天买入股数:500
【输出】
张三目前持有股数为:1500
【代码示例】
def he(x,y):
print("张三原持有股数:",x)
print("张三今天买入股数:",y,sep="")
print("张三目前持有股数:",x+y,sep="")
x, y = eval(input("请输入张三原有的股数和今天购入的股数:"))
he(x,y)
【终端输出】
张三原持有股数: 1000
张三今天买入股数:500
张三目前持有股数:1500
sep
是print函数的参数。
sep=""
表示值与值之间的分隔符,默认是一个空格。
print("张三今天买入股数:",y,sep="")
上述代码中要输出的内容有2部分:
第1部分是字符串:"张三今天买入股数:"
。
第2部分是要输出的变量y
。
print函数中增加了sep=""
表示要输出的2个部分之间不用分隔。
如果没有sep参数,则默认要输出的多个值之间用空格分隔。
第2和第3个print语句中我增加了sep=""
,表示前面的提示语句和后面的值之间不用分隔。
大家观察第1个和第2个print语句的输出效果就能看到区别。
eval
函数可以实现从终端一次性输入多个值,并且能将input
函数接收到字符串类型数据转变为能进行数学运算的数据。
eval函数原文链接如下:
80.Python eval函数-执行一个字符串表达式并返回执行结果
【函数的调用语法】
函数名(参数)
he(x, y)
【温馨提示】
input函数从终端输入多个值时,多个值之间用英文逗号分隔。终端输入如下所示:
1000,500
【备注】
不是我把简单的问题复杂化,这么简单的问题非要弄一个自定义函数来解决。
是学到这里我们要慢慢的增加难度,之后的题大家尽量用自定义函数来解决。
从简单的题慢慢深入,为以后做项目打下基础。
2. 乘法运算
【目标任务】
1960-1978年,我国GDP从1470.1亿元增长至3678.7亿元。
1978-2020年,我国GDP从3678.7亿元增长至1015986.2亿元。
计算输出上述2个阶段我国GDP年平均增长额。
【体验代码】
def ave(x, y, z):
print("1960年GDP:",x, sep="")
print("1978年GDP:",y,sep="")
print("2020年GDP:",z,sep="")
print("1960-1978年GDP平均每年增长额为:",(y-x)/(1978-1960),sep="")
print("2020-1978年GDP平均每年增长额为:",(z-y)/(2020-1978),sep="")
x, y, z= eval(input("请分别输入1960年,1978年,2020年的GDP数值:"))
ave(x,y,z)
【终端输出】
1960年GDP:1470.1
1978年GDP:3678.7
2020年GDP:1015986.2
1960-1978年GDP平均每年增长额为:122.69999999999999
2020-1978年GDP平均每年增长额为:24102.559523809523
average[ˈævərɪdʒ]:平均值。
3. 保留2位小数
上面的代码得到的结果小数位太多,我们现在对上面的代码进行优化,只保留2位小数。
保留小数位数通常有4中方法,可参考下面的链接:
【错误代码示例】
def ave(x, y, z):
print("1960年GDP:",x, sep="")
print("1978年GDP:",y,sep="")
print("2020年GDP:",z,sep="")
print("1960-1978年GDP平均每年增长额为:",format((y-x)/(1978-1960),2),sep="")
print("2020-1978年GDP平均每年增长额为:",format((z-y)/(2020-1978),2),sep="")
x, y, z= eval(input("请分别输入1960年,1978年,2020年的GDP数值:"))
ave(x,y,z)
这是很多初学者容易犯的错误。
大家仔细观察上面的代码问题出在哪里?
运行上面的代码,程序提示:
format() argument 2 must be str, not int
译为format()参数2必须是str,而不是int
。
即format的参数2必须是字符串类型,不能是整数类型。
format[ˈfɔːmæt]:格式化。
format((y-x)/(1978-1960),2)
将上面的代码修改如下:
format((y-x)/(1978-1960),'.2f')
-
首先是增加一个英文引号。
-
在2之前增加一个英语小圆点。
-
在2之后增加一个
f
。
【正确代码示例】
def ave(x, y, z):
print("1960年GDP:",x,sep="")
print("1978年GDP:",y,sep="")
print("2020年GDP:",z,sep="")
print("1960-1978年GDP平均每年增长额为:",format((y-x)/(1978-1960),'.2f'),sep="")
print("2020-1978年GDP平均每年增长额为:",format((z-y)/(2020-1978),'.2f'),sep="")
x, y, z= eval(input("请分别输入1960年,1978年,2020年的GDP数值:"))
ave(x,y,z)
【终端输出】
1960年GDP:1470.1
1978年GDP:3678.7
2020年GDP:1015986.2
1960-1978年GDP平均每年增长额为:122.70
2020-1978年GDP平均每年增长额为:24102.56
【温馨提示】
建议大家保留自己编辑代码时常犯的错误,有空时看几次,这样可以就能避免重复犯同样的错误。
4. 等额本息代码
房贷的还款方式有2种:等额本金和等额本息。
【等额本息】
【目标任务】
用input
函数从终端输入的方式计算输出等额本息法每月要还的贷款额。
【贷款】
84万元
【按揭】
30年=30*12=360个月
【贷款利率】
4.90%=0.049
【思路解析】
当我们水平有限不能一气呵成代码的时候,我们先把题目简单化处理。
题目要求用input函数从终端输入,这里我们先改成计算已知变量,降低题目难度。
total = 840000 # 贷款总额
month = 360 # 贷款时限360个月
rate_year = 0.049 # 年利率
rate_month = rate_year/12 # 月利率=年利率除以12(月)
print("月利率为:", rate_month, sep="")
# pay为每月要还的贷款额
# 本金 * 月利率 * (1+月利率)**贷款时限 / (1+月利率)**贷款时限 - 1
pay = (840000 * rate_month * (1+rate_month)**month) /((1+rate_month)**month-1)
print("每月还款额:", pay)
print("每月还款额:", format(pay,'.2f'))
print("每月还款额:", type(format(pay,'.2f')))
【终端输出】
月利率为:0.004083333333333334
每月还款额: 4458.104453231563
每月还款额: 4458.10
每月还款额: <class 'str'>
rate[reɪt]:费率,利率。
【format( )函数保留小数位数】
print(format(3.1415926, '.3f'))
【终端输出】
3.142
【代码示例】
print("每月还款额:", format(pay,'.2f'))
format后面的第一个参数是要格式化的对象,这里表示要对变量pay进行格式化。
'.2f'
表示保留2位小数。
【format函数得到的数据类型为字符串类型】
print("每月还款额:", type(format(pay,'.2f')))
【终端输出】
每月还款额: <class 'str'>
format函数得到的数据类型为字符串类型,这是需要重点关注的问题。
【**
表示幂运算】
print(2**3)
print(2**4)
【终端输出】
8
16
**
表示幂运算。
2**3
表示2的3次方。
【用自定义函数改写上面的代码】
增加一个要求:月利率保留4位小数。
【错误代码示例】
def cal(total, rate_month, month) :
# 本金 * 月利率 * (1+月利率) ** 贷款时限 / (1+月利率)**贷款时限 - 1
pay = (total*rate_month*(1+rate_month)**month)/((1+rate_month)**month-1)
print(pay)
total = eval(input("请输入贷款总额:"))
year = eval(input("请输入贷款年限:"))
month = year*12 # 贷款时限(月)
rate_year = eval(input("请输入贷款年利率:"))
rate_month = rate_year/12 # 月利率=年利率除以12(月)
rate_month = format(rate_month, '.4f') # 保留4位小数
print("贷款总额为:",total,sep="")
print("贷款年限为:",year,sep="")
print("贷款年利率:",rate_year,sep="")
print("贷款月利率为:",rate_month,sep="")
print("贷款月数:",month,sep="")
cal(total, rate_month, month)
【终端输出】
贷款总额为:840000
贷款年限为:30
贷款年利率:0.049
贷款月利率为:0.0041
贷款月数:360
TypeError: unsupported operand type(s) for +: 'int' and 'str'
运行上面的代码,程序报错:
unsupported operand type(s) for +: 'int' and 'str'
翻译为不支持+:“int”和“str”的操作数类型
。
说人话就是整数类型和字符串类型之间不能进行运算。
rate_month = format(rate_month, '.4f') # 保留4位小数
问题就出现在上面这行代码中。
我修改下上面的代码,给每个变量都增加一个数据类型。
【体验代码】
def cal(total, rate_month, month) :
# 本金 * 月利率 * (1+月利率) ** 贷款时限 / (1+月利率)**贷款时限 - 1
pay = (total*rate_month*(1+rate_month)**month)/((1+rate_month)**month-1)
print(pay)
total = eval(input("请输入贷款总额:"))
year = eval(input("请输入贷款年限:"))
month = year*12 # 贷款时限(月)
rate_year = eval(input("请输入贷款年利率:"))
rate_month = rate_year/12 # 月利率=年利率除以12(月)
rate_month = format(rate_month, '.4f') # 保留4位小数
print("贷款总额为:",total,sep="")
print(type(total))
print("贷款年限为:",year,sep="")
print(type(year))
print("贷款年利率:",rate_year,sep="")
print(type(rate_year))
print("贷款月数:",month,sep="")
print(type(month))
print("贷款月利率为:",rate_month,sep="")
print(type(rate_month))
cal(total, rate_month, month)
【终端输出】
贷款总额为:840000
<class 'int'>
贷款年限为:30
<class 'int'>
贷款年利率:0.049
<class 'float'>
贷款月数:360
<class 'int'>
贷款月利率为:0.0041
<class 'str'>
【输出示例】
贷款月利率为:0.0041
<class 'str'>
大家看最后一行输出,贷款月利率为rate_month
的数据类型为字符串。
pay = (total*rate_month*(1+rate_month)**month)/((1+rate_month)**month-1)
而上面的计算公式里含有变量rate_month
,因此程序才报错:整数类型和字符串类型之间不能进行运算。
【温馨提示】
涉及到数学运算的,如果我们的代码中有用到字符串格式化语法的,大家一定要记得对数据进行转换。
把字符串类型的数据转换为浮点数,我们用float函数就能实现。
继续修改上面的代码:
将下面的代码
rate_month = format(rate_month, '.4f') # 保留4位小数
修改如下:
rate_month = float(format(rate_month, '.4f')) # 保留4位小数
【最终代码】
def cal(total, rate_month, month) :
# 本金 * 月利率 * (1+月利率) ** 贷款时限 / (1+月利率)**贷款时限 - 1
pay = (total*rate_month*(1+rate_month)**month)/((1+rate_month)**month-1)
print("每月还款额为:",pay, sep="")
print("每月还款额为:",format(pay, '.2f' ), sep="")
total = eval(input("请输入贷款总额:"))
year = eval(input("请输入贷款年限:"))
month = year*12 # 贷款时限(月)
rate_year = eval(input("请输入贷款年利率:"))
rate_month = rate_year/12 # 月利率=年利率除以12(月)
rate_month = float(format(rate_month, '.4f')) # 保留4位小数
print("贷款总额为:",total,sep="")
print(type(total))
print("贷款年限为:",year,sep="")
print(type(year))
print("贷款年利率:",rate_year,sep="")
print(type(rate_year))
print("贷款月数:",month,sep="")
print(type(month))
print("贷款月利率为:",rate_month,sep="")
print(type(rate_month))
cal(total, rate_month, month)
【终端输出】
贷款总额为:840000
<class 'int'>
贷款年限为:30
<class 'int'>
贷款年利率:0.049
<class 'float'>
贷款月数:360
<class 'int'>
贷款月利率为:0.0041
<class 'float'>
每月还款额为:4468.3214300892
每月还款额为:4468.32
【总结】
Python一定要实操,代码一定要自己敲。
很多时候都是眼睛一看很简单,但手敲就会遇到各种各样的报错。只有多敲才能彻底掌握哦。
直接运算得到的月还款额为4458.10。
自定义函数得到的月还款额为4468.32。
两个数据不一致是因为自定义函数中的月利率我进行了保留4位小数的运算。