diff --git a/Work/bounce.py b/Work/bounce.py index 3660ddd82..b609a0be0 100644 --- a/Work/bounce.py +++ b/Work/bounce.py @@ -1,3 +1,13 @@ # bounce.py # # Exercise 1.5 + +current_height = 100 +bounce_factor = 3 / 5 +bounce_number = 1 + + +while bounce_number <= 10: + current_height = current_height * bounce_factor + print(bounce_number, round(current_height, 4)) + bounce_number += 1 diff --git a/Work/mortgage.py b/Work/mortgage.py index d527314e3..35a1030f1 100644 --- a/Work/mortgage.py +++ b/Work/mortgage.py @@ -1,3 +1,29 @@ # mortgage.py # # Exercise 1.7 + +principal = 500000.0 +rate = 0.05 +payment = 2684.11 +extra_payment = 1000 +extra_payment_start_month = 61 +extra_payment_end_month = 108 +total_paid = 0.0 +month = 1 + +while principal > 0: + if month <= extra_payment_end_month and month >= extra_payment_start_month: + monthly_payment = payment + extra_payment + else: + monthly_payment = payment + principal = principal * (1 + rate / 12) + if principal >= monthly_payment: + principal = principal - monthly_payment + else: + monthly_payment = principal + principal = 0 + total_paid = total_paid + monthly_payment + print(month, total_paid, principal) + month += 1 + +print("Total paid", total_paid) diff --git a/Work/pcost.py b/Work/pcost.py index e68aa20b4..65f90c298 100644 --- a/Work/pcost.py +++ b/Work/pcost.py @@ -1,3 +1,25 @@ # pcost.py # # Exercise 1.27 + +import os +import csv + + +def portfolio_cost(filename): + "returns total cost of the portfolio from filename" + total_cost = 0 + with open(filename, "rt") as f: + rows = csv.reader(f) + next(rows) + for l in rows: + try: + [shares, price] = l[1:] + total_cost = total_cost + int(shares) * float(price) + except: + print("Could not parse:", l) + return total_cost + + +total_cost = portfolio_cost("Data/missing.csv") +print("Total cost:", total_cost)