为了账号安全,请及时绑定邮箱和手机立即绑定

在 Python 3 中使用多个输入变量

在 Python 3 中使用多个输入变量

慕的地10843 2021-11-16 10:35:13
所以我对编码很陌生,我正在学习 python,所以我决定尝试制作一个贷款计算器。我这样做是为了当用户输入他们的本金、利率和全额支付贷款所需的年数时,它会输出他们的年付款、月付款和贷款总付款。我做了这个,它奏效了。我决定更进一步,让它在此之后,如果用户输入他们的年收入,它会将他们的月收入与他们的月付款进行比较,并告诉他们是否需要再融资。这是我制作的程序:principle = float(input("Principle: ")) #principle = the amount of dollars borrowedrate = float(input("Rate: ")) #rate = the interest rate that is charged each year on unpaid principleyears = float(input("Years: ")) #years = the number of years required to repay the loan in fullpayment = ((1 + rate)**years * principle * rate)/((1 + rate)**years - 1)#lines 7-10 print the annual, monthly, and total payments made respectivelyprint("Annual payment: ${:,.2f}".format(payment))print("Monthly payment: ${:,.2f}".format(payment/12))print("Total paid for the life of the loan: ${:,.2f}".format(payment*years))principle = float(input("Principle: ")) #principle = the amount of dollars borrowedrate = float(input("Rate: ")) #rate = the interest rate that is charged each year on unpaid principleyears = float(input("Years: ")) #years = the number of years required to repay the loan in fullpayment = ((1 + rate)**years * principle * rate)/((1 + rate)**years - 1)annualinc = float(input("Annual income: ")) #annualinc = the annual income#to check if the user needs to refinance or not by comparing their monthly income to their monthly paymentif (annualinc / 12) <= (payment / 12) and rate > .05:    print("You should refinance")elif (annualinc / 12) <= (payment / 12):    print("You should seek financial counseling")else:    print("If you make all your payments, your loan will be paid on time.")让 if 语句起作用的唯一方法是让用户重新输入打印语句和 if 语句之间的每个变量。每当我将变量annualinc = float(input("Annual income: ")放在打印语句之前或打印语句和 if 语句之间的程序开头时,它都会用语法错误打破它后面的行。为什么我必须再次询问所有变量,为什么我不能单独询问变量annualinc?为什么当我把它和第一组变量放在一起时它不起作用?编辑:我修复了它,所以我不必再次输入所有变量!我在行尾丢失了一个括号,当我移动它时我一直在复制和粘贴该行,因此错误随之而来。很抱歉出现这样的菜鸟错误,谢谢!
查看完整描述

1 回答

?
蝴蝶不菲

TA贡献1810条经验 获得超4个赞

这适合你吗?


principle = float(input("Principle: ")) #principle = the amount of dollars borrowed

rate = float(input("Rate: ")) #rate = the interest rate that is charged each year on unpaid principle

years = float(input("Years: ")) #years = the number of years required to repay the loan in full


payment = ((1 + rate)**years * principle * rate)/((1 + rate)**years - 1)


#lines 7-10 print the annual, monthly, and total payments made respectively

print("Annual payment: ${:,.2f}".format(payment))

print("Monthly payment: ${:,.2f}".format(payment/12))

print("Total paid for the life of the loan: ${:,.2f}".format(payment*years))


annualinc = float(input("Annual income: ")) #annualinc = the annual income


#to check if the user needs to refinance or not by comparing their monthly income to their monthly payment

if (annualinc / 12) <= (payment / 12) and rate > .05:

    print("You should refinance")

elif (annualinc / 12) <= (payment / 12):

    print("You should seek financial counseling")

else:

    print("If you make all your payments, your loan will be paid on time.")

我刚刚消除了多余的部分,它可以在我的机器上运行!


查看完整回答
反对 回复 2021-11-16
  • 1 回答
  • 0 关注
  • 250 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信