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

2024-10-02 12:34:13 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我对编码非常陌生,我正在学习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语句之间时,它就会因为语法错误而打断后面的行。为什么我必须再次要求所有的变量,为什么我不能只要求变量annualinc本身?为什么当我把它和第一组变量放在一起时它就不起作用了

编辑:我修正了它,这样我就不必再输入所有的变量了!我在行尾缺少了一个圆括号,我一直在复制和粘贴这行,当我移动它时,错误就随之而来了。对于这样一个新手的错误,我深表歉意,谢谢你


Tags: ofthetoinputifrate语句payment
1条回答
网友
1楼 · 发布于 2024-10-02 12:34:13

这个适合你吗

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

我刚刚消除了多余的部分,它在我的机器上工作

相关问题 更多 >

    热门问题