抵押贷款计算器不工作

时间:2015-07-29 16:50:48

标签: python python-3.x

这是我的代码,它不会给出正确答案:

print("Hello and welcome to Up-to-the Mark Mortgage Calculator!")
lamt = input("\n\n please enter the loan amount.")
intRate = input("please enter the interest rate.")
numOy = input("please enter the number of years.")

L = lamt
i = intRate
n = numOy

mortgageAmt = int(int(L)* (int(i) * (1+int(i)) * (int(n))/12) / ((1 +int(i))*(int(n)/12)-1))
print(int(mortgageAmt))
input("prompt:")

1 个答案:

答案 0 :(得分:1)

你为什么迷恋int

如果您提示用户输入某个号码,但他们不会这样做,除非您使用try: except:阻止,否则会导致您的程序崩溃。这可能就是为什么它没有正确地做到这一点。 int不考虑小数,如果您想要小数,请使用float。一般的想法是将您的问题包装到try块中except ValueError:,这样,如果他们没有输入数字,它将退出程序,而不是给你一个堆栈跟踪。我不知道你试图做什么配方,也不知道是每月付款还是每年付款。所以我使用了每月按揭付款公式。

try:
    lamt = float(input("\n\n please enter the loan amount: "))
    intRate = float(input("please enter the interest rate:")) #as decimal, not percentage.
    numOy = int(input("please enter the number of years: "))

    L = lamt
    i = intRate
    n = numOy

    mortgageAmt = (i*L)/((1 + i)**n - 1) #formula for monthly mortgage payments
    print(mortgageAmt)

except ValueError:
    print ("please use a proper numbers.")
相关问题