The copy-paste wall
Worked examples
Read the claim, then run it and check the machine agrees. One at a time — nothing here is taken on trust.
book_a = [30, 55, 20, 80]
total_a = 0
big_a = 0
for n in book_a:
total_a += n
if n > 50:
big_a += 1
frac_a = big_a / len(book_a)
print("total:", total_a)
print("fraction over 50:", frac_a)
The building blocks from this module in one place: an accumulator (total_a),
a counter with an if (big_a), and a fraction at the end. Book A totals
185, with 2 of 4 trades over 50 — a fraction of 0.5. Everything you need.
The only problem is what comes next: doing it again.
book_a = [30, 55, 20, 80]
book_b = [10, 5, 90]
book_c = [60, 62, 61, 5, 5]
total_a = 0
for n in book_a:
total_a += n
total_b = 0
for n in book_b:
total_b += n
total_c = 0
for n in book_c:
total_c += n
print(total_a, total_b, total_c)
print("grand total:", total_a + total_b + total_c)
Three loops, byte-for-byte identical except for one letter. They give 185,
105, 193, and a grand total of 483 — all correct. But squint: this is
one idea written three times. If the rule changed, you'd edit it in three
places and pray you didn't miss one. Hold that discomfort — Module 3 removes
it entirely.
Check the concept
One question at a time. Unsure? Revisit the lecture, then answer.
The challenge
Pass the quiz to unlock the challenge — your code will still be waiting here.