2017-01-29 7 views
-4

웹 사이트에서 대부 계산기를 만들려고하고 있는데 파이썬 코딩에 문제가 있습니다. 코드는 다음과 같습니다.대부 계산기의 파이썬 코딩 문제

# user enter the cost of the loan, the interest rate, and 
#the number of years for the loan 
#Calculate monthly payments with the following formula 
# M = L[i(1+i)n]/[(1+i)n-2] 
# M = monthly payment 
# L = Loan amount 
# i = interest rate (for an interest rate of 5%, i = 0.05) 
# n = number of payments 
#________________________________________________________________________# 
#Start of program 
#Declare variables 

monthlypayment = 0 
loanamount = 0 
interestrate = 0 
numberofpayments = 0 
loandurationinyears = 0 
loanamount = raw_input("Lending Money ") 
interestrate = raw_input("Interest Rates are? ") 
loandurationinyears = raw_input("Time Duration in Years?") 
#Convert the strings into floating numbers so we can use them in the formula 
loandurationinyears = float(loandurationinyears) 
loanamount = float(loanamount) 
interestrate = float(interestrate) 
#Since payments are once per month, number of payments is number of years for the loan 
payments = loaninyears*12 
#calculate the monthly payment based on the formula 
payment = amount * interestrate * (7+ interestrate) * payments/((1 + interestrate) * payments -1) 
#Result to the program 
print("Payment will be " + st(monthlypayment)) 

경험이있는 사람이이 코딩에서 구문이나 다른 논리적 오류를 얻을 수 있도록 도와 줄 수 있습니까?

+0

가'loaninyears'가 정의되지 않은 you_may_place_some_underscores_in_them. 너는 12를 곱하고있다. – MYGz

+0

'print ("지불은"+ st (monthlypayment))'나는'st'가 여기에'str'이어야한다고 생각한다. – MYGz

+0

amount는 아마 loanamount이어야하고 결코 0 이외의 값을주지 않는다. –

답변

1

이전에 선언하지 않은 변수를 읽는 중입니다. loaninyears에게에 loandurationinyears loanamount로 변경 . 또한

마지막 줄에 오타가, 는 str을 또한

몇 가지 팁을해야한다 :

첫째, 당신이 할 수있는 것들과 같은 :

input = float(raw_input("Give me some number")) 

이 방법으로 프로그램 길이를 줄일 수 있습니다.

또한

당신은 예를 들어, 더 읽기 변수 이름을 사용하는 것을 고려 할 수 있습니다 :

loanInYears 또는 밀접하게 코멘트에 수식에 따라 loan_in_years

0

및 파이썬 2.7 사용이 실행 , 결과는 정확하지 않습니다.

# user enter the cost of the loan, the interest rate, and 
#the number of years for the loan 
#Calculate monthly payments with the following formula 
# M = monthly payment 
# L = Loan amount 
# i = interest rate (for an interest rate of 5%, i = 0.05) 
# n = number of payments 

L = input ('loan amount') 
i = input ('interest rate') 
n = input ('nr of payments') 

M = L*(i*(1+i)*n)/((1+i)*n-2) 

print (M) 

코딩 오류를 제외하고 먼저 공식을 수정해야한다고 생각합니다. 특히 관심 분야가 일년에 12 번이므로 어딘가에서 12 번을 놓치지 만 월별 금액은 납부합니다. 여기 Joshy의 대답에

[편집]

찾는이 하나가 문제가 많은 사람을 얻을 것으로 보인다 이후

Formula for calculating interest Python

아마도 다른 비디오 자습서를 사용합니다.

팁 : ifyouhaveverylongvariablenames는