CS1301xl Вычисления в Python Я практикую экзамен формула проблемы с ипотекой может быть как-то неверно? - PullRequest
0 голосов
/ 03 июля 2019

Я хотел бы знать, это проблема формулы или моя проблема.

Я просмотрел различные формулы в Интернете. Это формула edx Стоимость * Количество месяцев * Ежемесячная ставка / 1 - ((1 + Ежемесячная ставка) ** Количество месяцев)

cost = 150000
rate = 0.0415 
years = 15
rate = rate / 12 
years = years * 12
house_value = cost * years * rate 
house_value2 = (1 + rate) ** years
house_value = house_value / house_value2
house_value = round(house_value, 2)
print("The total cost of the house will be $" + str(house_value))

На нем должно быть напечатано: «Общая стоимость дома составит $ 201751,36», но напечатано «Общая стоимость дома - $ 50158,98»

.

Ответы [ 2 ]

0 голосов
/ 03 июля 2019

Отвечая на ваш ответ правильной формулой, вы можете немного упростить код и повысить читабельность, выполнив следующие действия:

# This is a function that lets you calculate the real mortgage cost over
# and over again given different inputs.
def calculate_mortgage_cost(cost, rate, years):
    # converts the yearly rate to a monthly rate
    monthly_rate = rate / 12
    # converts the years to months
    months = years * 12
    # creates the numerator to the equation
    numerator = cost * months * monthly_rate
    # creates the denominator to the equation
    denominator = 1 - (1 + monthly_rate) ** -months
    #returns the calculated amount
    return numerator / denominator


# sets the calculated amount
house_value = calculate_mortgage_cost(150000, 0.0415, 15)

# This print statement utilizes f strings, which let you format the code
# directly in the print statement and make rounding and conversion
# unnecessary. You have the variable inside the curly braces {}, and then 
# after the colon : the comma , adds the comma to the number and the .2f
# ensures only two places after the decimal get printed.
print(f"The total cost of the house will be ${house_value:,.2f}")
0 голосов
/ 03 июля 2019
I have now solved this. This is the edit.
cost = 150000
rate = 0.0415 
years = 15
rate = rate / 12 
years = years * 12
house_value = cost * years * rate 
house_value2 = 1 - (1 + rate) ** -years
house_value = house_value / house_value2
house_value = round(house_value, 2)
print("The total cost of the house will be $" + str(house_value))
I have added a negative sign to the years.   
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...