2017-01-29 2 views
-4

Я пытаюсь создать калькулятор ссуды на веб-сайте, и у меня возникают проблемы с кодированием Python. Код:Ошибка кодирования Python для калькулятора займов

# 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' не определен. Вы умножаете его на 12. – MYGz

+0

'print (« Оплата будет »+ st (ежемесячный платеж))' Я думаю, что 'st' должно быть' str' здесь – MYGz

+0

сумма должна, вероятно, быть ссудой, и вы никогда не даете ежемесячный платеж за значение, отличное от 0 –

ответ

1

Вы читаете переменные, которые вы ранее не объявляли. Изменение loaninyears в loandurationinyears и количество к loanamount.

Кроме того у вас есть опечатка в последней строке, ул должна быть ул

Кроме того, некоторые советы:

Во-первых, вы можете сделать такие вещи, как:

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

Этом вы можете сократить продолжительность своей программы.

Кроме того, вы можете рассмотреть возможность использования более читаемое переменное именования, например:

loanInYears или loan_in_years

0

После вашей формулы в комментариях близко, и с помощью Python 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 где-то, так как ваш интерес в год, но ваши платежи в месяц.

[EDIT]

Посмотрите ответ на Джоши здесь:

Formula for calculating interest Python

и, возможно, использовать различные видео-учебник, так как это, кажется, чтобы получить много людей в беде.

Совет: ifyouhaveverylongvariablenames you_may_place_some_underscores_in_them