Search 72,520 tutors

# Exercise Answers to Chapter 2 of How to Think Like a Computer Scientist Learning with Python 3 (RLE)

1. Take the sentence: All work and no play makes Jack a dull
boy. Store each word in a separate variable, then print out the
sentence on one line using print.

#! /usr/bin/env python3

# ex1 - words

# All work and no play makes Jack a dull boy.

word1 = "All"
word2 = "work"
word3 = "and"
word4 = "no"
word5 = "play"
word6 = "makes"
word7 = "Jack"
word8 = "a"
word9 = "dull"
word10 = "boy."

print(word1, word2, word3, word4, word5,
word6, word7, word8, word9, word10)

2. Add parenthesis to the expression 6 * 1 - 2 to change its value from 4
to -6.

#! /usr/bin/env python3

print("6 * 1 - 2 = ", 6 * 1 - 2)
print("6 * (1 - 2) = ", 6 * (1 - 2))

3. Place a comment before a line of code that previously worked, and
record what happens when you rerun the program.

total_secs = int(input("How many seconds, in total?"))
hours = total_secs // 3600
secs_still_remaining = total_secs % 3600
# minutes = secs_still_remaining // 60
secs_finally_remaining = secs_still_remaining % 60

print("Hrs=", hours, " mins=", minutes,
"secs=", secs_finally_remaining)

# Traceback (most recent call last):
# File "G:\PADC\Documents\Python\Think CS Py3\exercises\ch 2\ex3_comment.py", line 7, in <module>
# print("Hrs=", hours, " mins=", minutes,
# NameError: name 'minutes' is not defined

4. Start the Python interpreter and enter bruce + 4 at the prompt. This
will give you an error:

NameError: name 'bruce' is not defined

Assign a value to bruce so that bruce + 4 evaluates to 10.

#! python3

# ex 2-4

# Start the Python interpreter and enter bruce + 4 at the prompt. This will give you an error:
#
# NameError: name 'bruce' is not defined
#
# Assign a value to bruce so that bruce + 4 evaluates to 10.

# print(bruce)
# print(bruce + 4)
bruce = 6
print(bruce + 4)

5. The formula for computing the final amount if one is earning compound
interest is given on Wikipedia as

formula for compound interest

Write a Python program that assigns the principal amount of \$10000 to
variable P, assign to n the value 12, and assign to r the interest
rate of 8%. Then have the program prompt the user for the number of
years t that the money will be compounded for. Calculate and print the
final amount after t years.

#! /usr/bin/env python3

# ex5 - interest

P = 10000 # principal in dollars
r = 8/100 # nominal interest rate
n = 12 # yearly compounding frequency
t = int(input("Please enter the number of years to save for: "))

A = P * (1 + r/n)**(n*t)

print("Your final amount is: ", A)

the Python interpreter to check your results:

5 % 2
9 % 5
15 % 12
12 % 15
6 % 6
0 % 7
7 % 0

What happened with the last example? Why? If you were able to
correctly anticipate the computer’s response in all but the last one,
it is time to move on. If not, take time now to make up examples of
your own. Explore the modulus operator until you are confident you
understand how it works.

#! /usr/bin/env python3

# >>> 5 % 2 == 1
# >>> 9 % 5 == 4
# >>> 15 % 12 == 3
# >>> 12 % 15 == 12
# >>> 6 % 6 == 0
# >>> 0 % 7 == 0
# >>> 7 % 0 => divide by zero error?

print("5 % 2 == ", 5 % 2, " == 1?")
print("9 % 5 == ", 9 % 5, " == 4?")
print("15 % 12 == ", 15 % 12, " == 3?")
print("12 % 15 == ", 12 % 15, " == 12?")
print("6 % 6 == ", 6 % 6, " == 0?")
print("0 % 7 == ", 0 % 7, " == 0?")
print("And now, a divide by zero error:")
print("7 % 0 == ", 7 % 0)

7. You look at the clock and it is exactly 2pm. You set an alarm to go
off in 51 hours. At what time does the alarm go off? (Hint: you could
count on your fingers, but this is not what we’re after. If you are
tempted to count on your fingers, change the 51 to 5100.)

answer is contained in solution to next problem

8. Write a Python program to solve the general version of the above
problem. Ask the user for the time now (in hours), and ask for the
number of hours to wait. Your program should output what the time will
be on the clock when the alarm goes off.

#! /usr/bin/env python3

# exercises 7 and 8 - compute the time in n hours, starting from 2pm

n = int(input("Enter the number of hours: "))
days = n // 24
timeafter = n % 24
if (timeafter > 10):
days = days + 1
hours = timeafter - 10
else:
hours = timeafter + 14

print("End time is in: ", days, " days, at ", hours, ":00 hours")