Skip to the content

Reviewing someone else's code

Python · Unit 20: Testing and debugging

Copying code you do not understand is borrowing a problem. Before using it, read it line by line and try it with a case whose answer you already know.

The average of 7 and 8 is 7.5, but this function says something else.

def average(ns):
    return sum(ns) // len(ns)

print(average([7, 8]))

Prints

7

The rest of the explanation is in the lesson, which is part of the full plan.

Exercises in this lesson

You do them in the app, which checks them on the spot and explains why.

  1. 1. Multiple choice

    What does this function actually return?

  2. 2. Predict the output

    What does this code print?

  3. 3. Find the bug

    An AI wrote this function to tell whether someone is an adult, and at 18 you already are. Which line has the error?

  4. 4. Find the case that fails

    You asked an AI for priciest(ps), which should return the highest price in the list, or 0 when the list is empty. Which call breaks it?

  5. 5. Find the bug

    An AI wrote this function to add up every number from 1 to n. Which line has the error?

  6. 6. Complete the code

    The rule says a 6 is already a pass. Complete the comparison.

  7. 7. Put the lines in order

    Put the review of passes in order: first one that clearly passes, then the exact limit, and last one that fails.

  8. 8. Predict the output

    What does this code print?

Do this lesson

It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.

See all lessons →