Code Labs: Core Constructs
Put Sequence, Selection, and Iteration into practice using Python.
Interactive Engine
Write Python code directly below. Press Run Code to compile your algorithms locally inside your browser.
User Input Required
The Crucible
Prove your knowledge of the 3 fundamental programming constructs.
The Chronology Crash
This programme triggers a massive NameError! The code is executing sequentially from top to bottom, but we are trying to output a variable before we even ask the user for it.
Task: Rearrange the lines of code so that it runs perfectly. The final line must print the favourite_colour variable.
The Bouncer's Logic
This cinema ticket system has a strict age rating of 15. The code attempts to use an IF/ELSE statement to branch the logic based on the user's age, but it has a lethal syntax error preventing it from running.
Task: Locate the missing punctuation and fix the comparison operator (the user must be 15 or over to watch the movie). The console must print Watch Move or Go Home.
The Infinite Loop Trap
You have been tasked with building an alarm countdown. You need to use a WHILE loop (condition-controlled iteration).
Task: Write a loop that prints the numbers 5, 4, 3, 2, 1 and then prints ALARM. Your loop MUST subtract 1 from the timer each time, otherwise you will crash your browser by creating an infinite loop!
while ( ... ):
print(timer)
timer = timer - 1
print("ALARM")