Chapter 13: Modules
10 Find the Bug exercises from Chapter 13 of Everyday Programming. Every program below is short, does something recognizable, and hides exactly one bug — syntax, runtime, or logical. Read it, predict what it does, then run it and find out.
Worked solutions for this chapter: Chapter 13 solutions. Every snippet is also available as a .py file — see Exercises & Solutions.
Exercises 13.1.1–13.1.5
Exercise 13.1.1 — Square root with math
This program should print the length of the hypotenuse of a right triangle with legs 3 and 4 (it should be 5.0).
import math
leg_a = 3
leg_b = 4
hypotenuse = math.sqrt(leg_a ** 2 - leg_b ** 2)
print(hypotenuse) # 5.0
Solution 13.1.1 · ex_13_1_1.py
Exercise 13.1.2 — Circle area with pi
This program should compute the area of a circle with radius 5 using π r².
import math
radius = 5
area = math.pi() * radius ** 2
print(area) # about 78.54
Solution 13.1.2 · ex_13_1_2.py
Exercise 13.1.3 — Rolling a die
This program should print a random whole number from 1 to 6, like rolling a single die.
import random
roll = random.randint(1, 7)
print(roll) # a number from 1 to 6
Solution 13.1.3 · ex_13_1_3.py
Exercise 13.1.4 — Average test score
This program should print the average of four test scores using the statistics module.
import statistics
scores = [80, 90, 100, 70]
average = statistics.mean(scores)
print(statistic.mean(scores)) # expected: 85
Solution 13.1.4 · ex_13_1_4.py
Exercise 13.1.5 — Rounding a price up
This program should round a price of $4.20 up to the next whole dollar using a from-import.
from math import ceil
price = 4.20
rounded_up = math.ceil(price)
print(rounded_up) # 5
Solution 13.1.5 · ex_13_1_5.py
Exercises 13.2.1–13.2.5
Exercise 13.2.1 — Importing your own helper
This two-file program should convert 100 degrees Celsius to Fahrenheit (212.0).
# file: conversions.py
def c_to_f(celsius):
return celsius * 9 / 5 + 32
# file: main.py
from conversions import c_to_k
print(c_to_f(100)) # 212.0
Solution 13.2.1 · ex_13_2_1.py
Exercise 13.2.2 — Calling with the module prefix
This two-file program should print the area of a 4-by-6 rectangle (24).
# file: geometry.py
def rectangle_area(width, height):
return width * height
# file: main.py
import geometry
print(rectangle_area(4, 6)) # 24
Solution 13.2.2 · ex_13_2_2.py
Exercise 13.2.3 — The main guard
This module should print a sample conversion only when run directly, but it should still work when imported.
# file: conversions.py
def c_to_f(celsius):
return celsius * 9 / 5 + 32
if __name__ = "__main__":
print(c_to_f(100)) # 212.0
Solution 13.2.3 · ex_13_2_3.py
Exercise 13.2.4 — Spelling the function name
This two-file program should print the perimeter of a square with side 5 (20).
# file: shapes.py
def square_perimeter(side):
return 4 * side
# file: main.py
import shapes
print(shapes.square_permieter(5)) # 20
Solution 13.2.4 · ex_13_2_4.py
Exercise 13.2.5 — Kelvin conversion formula
This two-file program should convert 0 degrees Celsius to Kelvin (273.15).
# file: conversions.py
def c_to_k(celsius):
return celsius - 273.15
# file: main.py
from conversions import c_to_k
print(c_to_k(0)) # 273.15