
Code
Measure the Depths
The math module holds the classic number tools.
import math
math.ceil(2.1) # 3 (round up)
math.floor(2.9) # 2 (round down)
math.sqrt(81) # 9.0 (square root)
math.pi # 3.14159...
math.prod([2, 3, 4]) # 24 (multiply them all)
Comparing decimals safely
Computers store decimals in binary. That means tiny errors can creep in:
0.1 + 0.2 == 0.3 # False! (0.30000000000000004)
math.isclose(0.1 + 0.2, 0.3) # True
math.isclose asks "are these two numbers almost equal?" Use it
whenever you compare floats.
Assignment
crates_needed(items, per_crate): return how many crates holditems. Round up withmath.ceil.lock_combinations(dials):dialsis a list of how many symbols each dial has. Return how many combinations the lock has: all the numbers multiplied together. Usemath.prod.same_reading(a, b): returnTrueif two float readings are equal according tomath.isclose.
crates_needed(items, per_crate): return how many crates holditems. Round up withmath.ceil.lock_combinations(dials):dialsis a list of how many symbols each dial has. Return how many combinations the lock has: all the numbers multiplied together. Usemath.prod.same_reading(a, b): returnTrueif two float readings are equal according tomath.isclose.
main.py Python sleeping
Press Run to try your code, then Check to test it.