Every station ends with practice problems that run your Python in the browser. This lesson explains what you will see there, and how to use it when your code does not work yet.
The practice screen
The problem is on the left. On the right is an editor with starter code, and a Run tests button. The starter code has the function name the tests look for. Keep that name and its parameters.
The first time you open practice, the browser downloads Python, which is about 10 MB. After that it is stored by the browser. Your code is saved in this browser as you type. Practice needs a laptop or desktop; on a phone you can read the problem and the hints.
Reading a test row
Each test calls your function once and compares what it returns with the expected answer. Results appear as rows in a timetable. Step through the figure to see a test run on a function with a hidden bug.
def largest(a):
best = 0
for x in a:
if x > best:
best = x
return best
| # | Test | Call | Expected | Got | Result |
|---|---|---|---|---|---|
| 1 | Largest in the middle | largest([4, 9, 2]) | 9 | Not run | |
| 2 | Largest at the end | largest([1, 3, 7]) | 7 | Not run | |
| 3 | One value | largest([5]) | 5 | Not run | |
| 4 | All negative | largest([-4, -2, -9]) | -2 | Not run | |
| 5 | Repeated values | largest([6, 6, 1]) | 6 | Not run |
This is the code under test. It looks fine. The practice screen runs it against each test in the table, one row at a time.
A red row opens to show four things:
- the input that was used;
- what your function returned, next to what was expected;
- anything your code printed;
- a short note on what this failure usually means. Every test has one. They are written by the course author for the mistakes students really make.
Rows run in order, from the simplest case to the hardest. Fix the first red row first. Later rows often fail for the same reason.
Read limits and time limits
Some tests check how fast your code is, not only whether it is right.
- Time limits. Each test has a few seconds. If your code is still running when the time is up, the row says “Timed out”. Either the code has a loop that never ends, or it does far too much work for a large input.
- Read limits. In some problems the input list counts how many values you read from it. A row then shows, for example, “20 / 25”: 20 reads, with a limit of 25. Going through the whole list goes over the limit, even if the answer is right.
- Switched-off tools. Some problems switch off a built-in tool, such as
sorted(), because the point is to build it yourself. The problem statement says so.
Hints and the solution
Each problem has three hints, opened one at a time. The first is a nudge in the right direction. The second describes the approach. The third is pseudocode: the steps in plain words, close to code. Open one, then try again before opening the next.
The full worked solution unlocks after all three hints are open. The course map records honestly that you saw it. That is fine: read it, close it, and write the code again yourself the next day.
Finding a bug with print
When a row fails and you cannot see why, add print lines to your function and run the tests again. The output appears in the failing row.
def largest(a):
best = 0
for x in a:
print("x =", x, "best =", best) # remove when fixed
if x > best:
best = x
return best
Now try the first practice problem: PYTH-P1, Rotate the departure board.
PYTH-6 counts as completed once you step through the figure to the last step.