Skip to content

Commit c07af72

Browse files
committed
Adding code for exercises in 1.3 Numbers
1 parent ba57e98 commit c07af72

4 files changed

Lines changed: 57 additions & 0 deletions

File tree

‎Work/bounce.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
# bounce.py
22
#
33
# Exercise 1.5
4+
height = 100 # metres
5+
num_bounces = 10
6+
i = 1
7+
8+
while i <= num_bounces:
9+
height = height * 3/5
10+
print(i, round(height, 4))
11+
i = i + 1

‎Work/hello.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
print('hello world')

‎Work/mortgage.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
11
# mortgage.py
22
#
33
# Exercise 1.7
4+
5+
principal = 500000.0
6+
rate = 0.05
7+
payment = 2684.11
8+
total_paid = 0.0
9+
10+
extra_payment_start_month = int(input('extra_payment_start_month: '))
11+
extra_payment_end_month = int(input('extra_payment_end_month: '))
12+
extra_payment = int(input('extra_payment: '))
13+
14+
current_month = 1
15+
num_required_months = 0
16+
17+
while principal > 0:
18+
if current_month >= extra_payment_start_month and current_month <= extra_payment_end_month:
19+
principal = principal * (1 + rate/12) - payment - extra_payment
20+
total_paid = total_paid + payment + extra_payment
21+
remaining_principal = principal if principal > 0 else 0
22+
print(current_month, 'total_paid:', total_paid, 'principal_remaining:', remaining_principal)
23+
else:
24+
principal = principal * (1 + rate/12) - payment
25+
total_paid = total_paid + payment
26+
remaining_principal = principal if principal > 0 else 0
27+
print(current_month, 'total_paid:', total_paid, 'principal_remaining:', remaining_principal)
28+
29+
current_month = current_month + 1
30+
num_required_months = num_required_months + 1
31+
32+
print('Total paid', total_paid)
33+
print('No. of months required to pay the mortgage', num_required_months)
34+
35+

‎Work/sears.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
bill_thickness = 0.11 * 0.001 # metres (0.11 mm)
2+
sears_height = 442 # height (metres)
3+
4+
day = 1
5+
num_bills = 1
6+
bills_height = 0.11
7+
8+
while bills_height < sears_height:
9+
print(day, num_bills, bills_height)
10+
day = day + 1
11+
num_bills = 2 * num_bills
12+
bills_height = num_bills * bill_thickness
13+
14+
print('No. of days:', day)
15+
print('No. of bills:', num_bills)
16+
print('Final bills height:', bills_height)

0 commit comments

Comments
 (0)