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

时间:2019-02-21 23:43:24

标签: python python-3.x variables syntax

所以我对编码非常陌生,而且我正在学习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语句之间的每个变量。每当我在程序的开头将变量annualinc = float(input("Annual income: ")放在print语句之前或在print语句与if语句之间时,都会在语法错误后在其后断行。为什么我不得不再次要求所有变量,为什么我不能只问变量变量incinc呢?当我将其与第一组变量一起使用时,为什么它不起作用?

edit:我已修复它,因此不必再次放入所有变量!我在该行的末尾缺少括号,并且在移动该行时一直复制并粘贴该行,因此错误随之发生。很抱歉出现这样的菜鸟错误,谢谢!!

1 个答案:

答案 0 :(得分: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.")

我刚刚删除了多余的零件,它可以在我的机器上工作!