我刚刚开始学习python,从java背景切换,我无法理解为什么我的数字不会在monthlyPayment下转。
loan = input("Please enter the loan amount.\n")
loan = int(loan)
interest = input("Please enter the interest rate.\n")
interest = int(interest)
years = input("Please enter the number of years desired.\n")
years = int(years)
i = interest/1200
monthlyPayment = (i/(1-(1+i)**(-12*years))*loan)
monthlyPayment = round(int(monthlyPayment),2)
print("With a loan of " + str(loan) + "$, an interest rate of " +
str(interest) + "%, and after " + str(years) + " years the monthly payment
totals at " + str(monthlyPayment) + "$.")
任何帮助了解我的错误发生的位置将不胜感激!
答案 0 :(得分:0)
通过使用int
函数将变量转换为整数,可以删除任何小数点。对整数进行舍入是没用的。
e.g。
int(10.33344443)
>> 10
如果您删除int
变量,则保留float
小数点前缀,以便您的脚本按预期运行。
e.g。
round(10.33344443,2)
>> 10.33