《python语言程序设计》_第二章笔记之2.13_软件开发流程

#程序1:

设计:由用户键入利率、贷款数以及贷款的年限,系统计算出每月还贷数和总还款数

注意:输入的年利率是带有百分比的数字,例如:4.5%。程序需要将它除以100转换成小数。因为一年有12个月,所以将年利率除以12即是月利率,为了获得月利率,月利率=年利率/(12+100)

程序:

 

annualInterestRate =eval(input("Enter annual interest rate,e.g., 7.25:"))
#输入年度利率
monthlyInterestRate=annualInterestRate / 1200
# 月利率=年利率/1200
numberOfYears = eval(input("Enter number of years as an integer,e.g.,5:"))
#输入贷款年限
loanAmount=eval(input("Enter loa amount,e.g.,120000.90:"))
#输入贷款数目
monthlyPayment =loanAmount * monthlyInterestRate/(1-1/(1+monthlyInterestRate)**(numberOfYears*12))
#定义月供(月还款数)
totalPayment=monthlyPayment*numberOfYears*12 #定义年付款额
print("The monthly payment is",int(monthlyPayment*100)/100)
print("THE total payment is",int(totalPayment*100)/100)
#先乘以100取整再除以100,是为了保留小数点后的两位。

结果:


Enter annual interest rate,e.g., 7.25:5.75

Enter number of years as an integer,e.g.,5:15

Enter loa amount,e.g.,120000.90:250000
The monthly payment is 2076.02
THE total payment is 373684.53

 

 程序2:计算距离(两点之间的距离最近)

 

 程序:

x1,y1=eval(input("Enter x1 and y1 for point1:"))
#输入第一个点的坐标x1,y1
x2,y2=eval(input("Enter x2 and y2 for point2:"))
#输入第二个点的坐标
distance =((x1-x2)**2+(y1-y2)**2)**0.5
#定义距离公式
print("The distance between the points is" , distance)

结果:

Enter x1 and y1 for point1:1.5,-3.4

Enter x2 and y2 for point2:4,5
The distance between the points is 8.764131445842194

 

posted @ 2018-08-28 21:57  张三疯了  阅读(270)  评论(0编辑  收藏  举报