File tree Expand file tree Collapse file tree 4 files changed +76
-0
lines changed Expand file tree Collapse file tree 4 files changed +76
-0
lines changed Original file line number Diff line number Diff line change 1
1
# bounce.py
2
2
#
3
3
# Exercise 1.5
4
+
5
+ height = 100
6
+ bounce_efficiency = 3 / 5
7
+
8
+ for bounce in range (1 , 11 ):
9
+ height = height * bounce_efficiency
10
+ print (bounce , round (height , 4 ))
Original file line number Diff line number Diff line change 1
1
# mortgage.py
2
2
#
3
3
# Exercise 1.7
4
+
5
+ principal = 500000.0
6
+ rate = 0.05
7
+ base_payment = 2684.11
8
+ total_paid = 0.0
9
+ month = 0
10
+
11
+ # Extra payments
12
+ extra_payment_start_month = 60
13
+ extra_payment_end_month = 108
14
+ extra_payment = 1000
15
+
16
+ while principal > 0 :
17
+ if month in range (extra_payment_start_month , extra_payment_end_month ):
18
+ payment = base_payment + extra_payment
19
+ else :
20
+ payment = base_payment
21
+ principal = principal * (1 + rate / 12 ) - payment
22
+ if principal < 0 :
23
+ payment += principal
24
+ principal = 0
25
+ total_paid = total_paid + payment
26
+ month += 1
27
+ print (f'{ month :>3d} { total_paid :>10.2f} { principal :>10.2f} ' )
28
+
29
+ print ('Total paid' , round (total_paid , 2 ))
30
+ print ('Months' , month )
Original file line number Diff line number Diff line change 1
1
# pcost.py
2
2
#
3
3
# Exercise 1.27
4
+
5
+ import csv
6
+ import sys
7
+
8
+
9
+ def portfolio_cost (filename ):
10
+ total = 0
11
+
12
+ with open (filename , 'rt' ) as f :
13
+ portfolio = csv .reader (f )
14
+ headers = next (portfolio )
15
+ for row in portfolio :
16
+ try :
17
+ symbol , shares , price = row
18
+ total += int (shares ) * float (price )
19
+ except ValueError :
20
+ print (f'Error parsing row: { row } ... Skipping...' )
21
+
22
+ return total
23
+
24
+
25
+ if len (sys .argv ) == 2 :
26
+ filename = sys .argv [1 ]
27
+ else :
28
+ filename = 'Data/portfolio.csv'
29
+
30
+ cost = portfolio_cost (filename )
31
+ print (f'Total cost: ${ cost :>7.2f} ' )
Original file line number Diff line number Diff line change
1
+ # sears.py
2
+ bill_thickness = 0.11 * 0.001 # Meters (0.11 mm)
3
+ sears_height = 442 # Height (meters)
4
+ num_bills = 1
5
+ day = 1
6
+
7
+ while num_bills * bill_thickness < sears_height :
8
+ print (day , num_bills , num_bills * bill_thickness )
9
+ day = day + 1
10
+ num_bills = num_bills * 2
11
+
12
+ print ('Number of days' , day )
13
+ print ('Number of bills' , num_bills )
14
+ print ('Final height' , num_bills * bill_thickness )
You can’t perform that action at this time.
0 commit comments