File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ language = "python3"
2+ run = ""
Original file line number Diff line number Diff line change 11# bounce.py
22#
33# Exercise 1.5
4+
5+ for i in range (10 ):
6+ print (str (i + 1 ) + ' ' + str (round (60.0 * .6 ** i , 4 )))
Original file line number Diff line number Diff line change 11# mortgage.py
22#
3- # Exercise 1.7
3+ # Exercises 1.7-11, 1.18
4+
5+ principal = 500000.0
6+ rate = 0.05
7+ payment = 2684.11
8+ total_paid = 0.0
9+ month = 0
10+ extra_payment_start_month = 60
11+ extra_payment_end_month = 108
12+ extra_payment = 1000
13+
14+ while principal > 0 :
15+ monthly_payment = payment
16+ if month >= extra_payment_start_month and month < extra_payment_end_month :
17+ monthly_payment += extra_payment
18+ month += 1
19+ if monthly_payment > principal :
20+ total_paid += principal
21+ principal = 0
22+ print (f'{ month } { round (total_paid , 2 ):6.2f} { round (principal , 2 ):6.2f} ' )
23+ break
24+
25+ principal = principal * (1 + rate / 12 ) - monthly_payment
26+ total_paid = total_paid + monthly_payment
27+ print (f'{ month } { round (total_paid , 2 ):6.2f} { round (principal , 2 ):6.2f} ' )
28+
29+ print (f'Total paid { round (total_paid , 2 ):6.2f} ' )
30+ print (f'Total months { month } ' )
Original file line number Diff line number Diff line change 11# pcost.py
22#
3- # Exercise 1.27
3+ # Exercise 1.27, 1.30-3
4+ import csv
5+ import sys
6+
7+ def portfolio_cost (filename ):
8+ total_cost = 0
9+ with open (filename , 'rt' ) as f :
10+ rows = csv .reader (f )
11+ headers = next (rows )
12+ for r in rows :
13+ try :
14+ total_cost += float (r [1 ]) * float (r [2 ])
15+ except ValueError :
16+ print ("WARN: the csv file is missing a value, skipping this row" )
17+ return total_cost
18+
19+ if len (sys .argv ) == 2 :
20+ filename = sys .argv [1 ]
21+ else :
22+ filename = 'Data/portfolio.csv'
23+ cost = portfolio_cost (filename )
24+ print (f'Total cost { round (cost , 2 ):0.2f} ' )
Original file line number Diff line number Diff line change 1+ # sears.py
2+ #
3+ # Exercise 1.6
4+
5+ bill_thickness = 0.11 * 0.001 # Meters (0.11 mm)
6+ sears_height = 442 # Height (meters)
7+ num_bills = 1
8+ day = 1
9+
10+ while num_bills * bill_thickness < sears_height :
11+ print (day , num_bills , num_bills * bill_thickness )
12+ day = day + 1
13+ num_bills = num_bills * 2
14+
15+ print ('Number of days' , day )
16+ print ('Number of bills' , num_bills )
17+ print ('Final height' , num_bills * bill_thickness )
You can’t perform that action at this time.
0 commit comments