A safe runtime runs short pieces of Python in your browser without network access or file access, and stops any program that runs too long. It is a place to test your reasoning, not a full computer.
This lesson belongs to Python transition and execution and builds on telling syntax errors from wrong results.
What does a restricted runtime allow and refuse?
| Allowed | Refused or outside its purpose |
|---|---|
| Variables, arithmetic, comparisons | Opening or saving files |
if, elif, else | Using the internet |
for and while loops | Loops that never end (stopped by a limit) |
Lists, functions, print | Heavy libraries and large data |
Our Python reasoning sandbox is built with a time limit and a memory limit, so a program that never ends is stopped instead of freezing the page. Keep real personal data out of browser tools.
Worked example
Task: print a countdown from 10 in steps of 3. Here is a first attempt.
n = 10
while n > 0:
print(n)
Step 1, trace. n starts at 10. The test 10 > 0 is True, so 10 is printed. Nothing changes n, so the test is True again, and 10 is printed again, forever.
Step 2, expect the limit. The runtime stops the program when its time limit is reached. The output is a long list of 10s followed by a stop message. The runtime stopping it is correct behaviour, and the fault is yours.
Step 3, repair. The loop must move n towards the exit.
n = 10
while n > 0:
print(n)
n = n - 3
Step 4, trace again.
| n at the test | n > 0 | printed | n after |
|---|---|---|---|
| 10 | True | 10 | 7 |
| 7 | True | 7 | 4 |
| 4 | True | 4 | 1 |
| 1 | True | 1 | -2 |
| -2 | False |
The output is 10, 7, 4, 1.
The mistake to watch for
A frequent slip is to write an exercise that depends on a file.
Mistaken answer:
lines = open("scores.txt").read()The runtime has no file access, so this fails. The mistake is in the exercise design, not in your logic.
The correction is to put the data in the program itself: scores = [72, 45, 50, 38, 91]. The algorithm is the same whether the numbers come from a file or a list, and in the exam you reason about the algorithm. Use invented values only.
Check yourself
1. Why does while True: with no way out not freeze the page in the sandbox?
Show answer
The runtime has a time limit and stops the program when it is reached, then shows a message.
2. Rewrite this so it works without a file: names = open("class.txt").read().
Show answer
names = ["Ali", "Mei", "Ravi"]. Use made-up names, and the rest of the algorithm stays unchanged.
3. What is output by this program?
n = 2
while n < 30:
n = n * 3
print(n)
Show answer
n goes 2, then 6, then 18, then 54. At 54 the test 54 < 30 is False, so the loop stops. Output: 54.
Where this leads next
Next, see why exam-year advice must be labelled in keeping 2029 Python-only guidance separate. Practise loop tracing in the restricted pseudocode trace trainer and then run the same logic in the Python reasoning sandbox.
If you would like guided practice on loops and testing, see our online one-to-one Computer Science tuition.