WEB PRACTICE CHAPTER 07

Debugging and Testing

Ten additional coding tasks · W061–W070

These extra website exercises are not a substitute for the book's exact numbered questions. Follow your printed book separately using the 100-challenge tracker.
Chapter 7 code

Choose starters to try yourself, or download a separate worked example pack after attempting the tasks.

Chapter 7 starters ↓Chapter 7 worked examples ↓
WEB PRACTICE W061

Off-by-one loop

Print 1 through 5, not 1 through 4.

Reveal a hint

range stops before its upper bound.

Reveal example code (after trying it yourself)
# Try this incorrect version first, then compare with the fix:
# for n in range(1, 5):
#     print(n)

for n in range(1, 6):
    print(n)

One possible solution. Try writing your own first.

Extension: Create two tests that would have caught this bug.

WEB PRACTICE W062

Wrong average

Calculate a three-score average rather than a total.

Reveal a hint

You must divide by the count.

Reveal example code (after trying it yourself)
# Try this incorrect version first, then compare with the fix:
# scores = [6, 9, 12]
# print(sum(scores))

scores = [6, 9, 12]
print(sum(scores) / len(scores))

One possible solution. Try writing your own first.

Extension: Create two tests that would have caught this bug.

WEB PRACTICE W063

Threshold boundary

Ensure a score of exactly 50 passes.

Reveal a hint

Use the inclusive comparison.

Reveal example code (after trying it yourself)
# Try this incorrect version first, then compare with the fix:
# score = 50
# print("Pass" if score > 50 else "Retry")

score = 50
print("Pass" if score >= 50 else "Retry")

One possible solution. Try writing your own first.

Extension: Create two tests that would have caught this bug.

WEB PRACTICE W064

List final index

Display the final element in a three-item list.

Reveal a hint

Length is not the final zero-based index.

Reveal example code (after trying it yourself)
# Try this incorrect version first, then compare with the fix:
# items = ["A", "B", "C"]
# print(items[len(items)])

items = ["A", "B", "C"]
print(items[len(items) - 1])

One possible solution. Try writing your own first.

Extension: Create two tests that would have caught this bug.

WEB PRACTICE W065

String comparison

Accept Yes, YES and yes as the same answer.

Reveal a hint

Convert to lowercase before comparing.

Reveal example code (after trying it yourself)
# Try this incorrect version first, then compare with the fix:
# answer = "YES"
# print(answer == "yes")

answer = "YES"
print(answer.lower() == "yes")

One possible solution. Try writing your own first.

Extension: Create two tests that would have caught this bug.

WEB PRACTICE W066

Running total placement

Accumulate the entire list instead of resetting on every turn.

Reveal a hint

Initialise total before the loop.

Reveal example code (after trying it yourself)
# Try this incorrect version first, then compare with the fix:
# values = [2, 3, 4]
# for x in values:
#     total = 0
#     total += x
# print(total)

values = [2, 3, 4]
total = 0
for x in values:
    total += x
print(total)

One possible solution. Try writing your own first.

Extension: Create two tests that would have caught this bug.

WEB PRACTICE W067

Wrong Boolean operator

Access requires BOTH helmet and glasses.

Reveal a hint

Use and, not or.

Reveal example code (after trying it yourself)
# Try this incorrect version first, then compare with the fix:
# helmet = True
# glasses = False
# print(helmet or glasses)

helmet = True
glasses = False
print(helmet and glasses)

One possible solution. Try writing your own first.

Extension: Create two tests that would have caught this bug.

WEB PRACTICE W068

Mutable list gotcha

Append an item without replacing the list with None.

Reveal a hint

append() modifies in place and returns None.

Reveal example code (after trying it yourself)
# Try this incorrect version first, then compare with the fix:
# items = ["pen", "book"]
# items = items.append("ruler")
# print(items)

items = ["pen", "book"]
items.append("ruler")
print(items)

One possible solution. Try writing your own first.

Extension: Create two tests that would have caught this bug.

WEB PRACTICE W069

Descending range

Count down correctly from 4 to 1.

Reveal a hint

Use a negative step in range.

Reveal example code (after trying it yourself)
# Try this incorrect version first, then compare with the fix:
# for n in range(4, 0):
#     print(n)

for n in range(4, 0, -1):
    print(n)

One possible solution. Try writing your own first.

Extension: Create two tests that would have caught this bug.

WEB PRACTICE W070

Percentage calculation

Calculate the £90 price after a 10% discount, not £9.

Reveal a hint

Multiply by 0.90 for a discounted price.

Reveal example code (after trying it yourself)
# Try this incorrect version first, then compare with the fix:
# price = 100
# print(price * 0.10)

price = 100
print(price * 0.90)

One possible solution. Try writing your own first.

Extension: Create two tests that would have caught this bug.