forked from DevRampUp/thirty_day_challenge
/
chapter2_exercises.py
73 lines (56 loc) · 1.85 KB
/
chapter2_exercises.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Exercises for chapter 2: Problems 2.1, 2.2, 2.3, and 2.4 in Think Python
# chrisvans - Chris Van Schyndel
#2.1
# zipcode = 02492
# print zipcode
# Results in an error because 9 is > 7, and an 8 (or more) would be
# represented by a number in the next place up.
# Each value in each place represents a octal place number,
# multiplied by the number provided. So in this case,
# zipcode = 02132
# The first 0 denotes that it is a octal number
# (0002) 2 * 1 + (0030) 3 * 8 + (0100) 1 * 64 + (2000) 2 * 512
#2.2
# 5 and x + 1 return nothing when run as a script. It is evaluated but
# nothing is done with it.
# x = 5 sets the variable x to integer 5.
# As print statements, 5 outputs 5, x = 5 outputs an error ( cannot assign
# during print statement ), x + 1 outputs 6.
#2.3
# 1. 8.5 - float
# 2. 8.5 - float
# 3. 4 - integer
# 4. 11 - integer
# 5. ..... - string
#2.4
# 1. 523.5987755982989
# 2. $945.45 ( rounded up from .44999~)
bookprice = 24.95
discount = .60
shipping_first = 3
shipping_additional = .75
books = 60
subtotal = shipping_first + bookprice * books + shipping_additional * (books - 1)
final = subtotal * discount
final
# This offers an answer if the discount applies to shipping, which it wouldn't.
alternate_final = (bookprice * books) * discount + shipping_first + shipping_additional * 59
alternate_final
# This is the answer for a discount on books before shipping.
# 3. 7:30.06 am
starting_time = 6 + 52/float(60)
# in hours
easy_pace = (8 + 15/float(60)) / 60
tempo = (7 + 12/float(60)) / 60
# in hours
hours = starting_time + easy_pace * 2 + tempo * 3
hours
# take the integer away from hours to find the minutes
minutes = hours - int(hours)
minutes *= 60
# convert back to time measurement
seconds = minutes - int(minutes)
seconds *= 60
# concatenate for answer
answer = str(int(hours)) + ':' + str(int(minutes)) + '.' + str(int(seconds))
answer