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

问题描述 投票:1回答:1

所以我对编码很新,我正在学习python,所以我决定尝试制作一个贷款计算器。我做到了这一点,当用户输入他们的原则,利率和完全支付贷款所需的年数时,它将输出他们的年度付款,他们的每月付款和他们的贷款总额。我做了这个,它工作。我决定更进一步,以便在此之后,如果用户输入他们的年收入,它会将他们的月收入与他们的月付款进行比较,并告诉他们是否需要再融资。

这是我制作的节目:

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))

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)

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.")

我能使if语句工作的唯一方法是让用户在print语句和if语句之间重新输入每个变量。每当我在打印语句之前或打印语句和if语句之间将变量annualinc = float(input("Annual income: ")放在程序的开头时,它就会在语法错误之后打破它之后的行。为什么我必须再次询问所有变量,为什么我不能单独询问变量annualinc?当我把它与第一组变量放在一起时,为什么它不起作用?

编辑:我修复它,所以我不必再次输入所有变量!我在线的末尾错过了一个括号,当我移动它时,我已经复制并粘贴线,因此误差随之移动。对不起这样的菜鸟错误,谢谢!

python python-3.x variables syntax
1个回答
0
投票

这适合你吗?

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.")

我刚刚删除了冗余部件,它可以在我的机器上运行!

© www.soinside.com 2019 - 2024. All rights reserved.