Last active
January 11, 2019 19:42
-
-
Save MarketaP/a00235c06df8efc481b52f166e2e265a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
""" | |
Read the comments in this file. The comments contain problem statements. | |
Do your best to write code that satisfies what is being requested in the | |
problem statements. | |
This homework will be handed in at the start of the next class by depositing | |
the file in the class folder in the Y drive. Place the homework in your | |
personal folder. Name the file {first name}_{last name}_homework1.py. | |
For example: philip_blankenau_homework1.py | |
""" | |
############################################################################### | |
# Write a function that coverts a Fahrenheit temperature to Celcius. | |
############################################################################### | |
fahrenheit = 32 #insert number that you want to convert | |
def conversion(fahrenheit): | |
'''Converts temperature in Fahrenheit to Celsius''' | |
return (fahrenheit - 32) / 1.8 | |
print(conversion(fahrenheit)) | |
############################################################################### | |
# Amortizied loan payments | |
# | |
# You purchased a used car for 13,000.00 with no down payment, so you take | |
# out a $13,000.00 loan. | |
# You want to know what the payments will be if you pay back your loan in 5 | |
# years, and you write the code below. The formula for the monthly payment | |
# is here: https://en.wikipedia.org/wiki/Amortization_calculator | |
# | |
# Convert the code below to a function or a functions you can reuse for other | |
# loans. | |
############################################################################### | |
principle = 13000.0 | |
annual_percentage_rate = 4.0 # % | |
years = 5.0 | |
payments_per_year = 12.0 | |
def monthly_interest(annual_percentage_rate, payments_per_year): | |
'''Calculates monthly interest''' | |
val = annual_percentage_rate / payments_per_year / 100.0 | |
return val | |
monthly_interest = monthly_interest(annual_percentage_rate, payments_per_year) | |
def total_num_payments(years, payments_per_year): | |
'''Calculates total number of payments needed to pay the loan''' | |
val = years * payments_per_year | |
return val | |
total_num_payments = total_num_payments(years, payments_per_year) | |
def monthly_payment(principle, monthly_interest, total_num_payments): | |
'''Calculates the monthly payment''' | |
val = (1 + monthly_interest) ** total_num_payments | |
return (principle * monthly_interest * val / (val - 1)) | |
print(round(monthly_payment(principle, monthly_interest, total_num_payments), 2)) | |
############################################################################### | |
# Uncomment the line where print(x) appears. Run the file and examine | |
# the output. | |
# Write to explain why there is a NameError "name 'x' is not defined" even | |
# though we defined x in the function. | |
# | |
# ANSWER: This gives you an error because variable x is defined only on a local scope, | |
# inside of a function. If print(x) was inside of that function it would return | |
# a value of 5, but since we are asking to print a value on a global scale where | |
# value x is not defined we get an error | |
# | |
# NOTE: Using x as a name in previous exercises can cause an issue here. | |
############################################################################### | |
def multiply_by_five(number): | |
"""Multiplies number by 5.""" | |
x = 5 | |
return number * x | |
multiply_by_five(5) | |
#print(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
monthly_interest = annual_percentage_rate / payments_per_year / 100.0
total_num_payments = years * payments_per_year
monthly_payment = (principle* monthly_interest* ((1 + monthly_interest) ** total_num_payments)
/ (((1 + monthly_interest) ** total_num_payments) - 1))
print(round(monthly_payment, 2))