3...2...1...lift off.
Select the button below to open the Python program in a new window. Run the program and read the lines of code to see if you can understand how it works. It will be helpful to arrange your display so that you can have this browser window on one side of the screen and the code on the other.
Watch this video to learn about the new concepts shown in the program:
The new commands used in this program and others that may be useful. Select them below to learn more:
Is used to repeat an indented section of code underneath the statement a known number of times.
Note The `while` command is used when you don't know how many iterations will be required, but `for` is used when the number of iterations is known.
Although you can use a while loop instead of a for loop as shown in the example below, it is not considered good practice unless the value of counter can be changed by another command in addition to the increment statement.
counter = 0
while counter < 5:
counter = counter + 1 # This is the increment statement.
Defines a range of numbers to be used by a for loop: x is the starting number, y is the finishing number and z is the increment/decrement applied to x to reach y in each iteration.
range(5)
Means the numbers 0 to 4. Note that it is not necessary to state the starting number or the increment if the loop starts at 0 and increases by 1 in each iteration.
Delays the next line of code executing for x number of seconds. Requires import time to access these commands.
import time
Includes functions from a library called `time` in your program. These are date, time and delay functions.
Questions to think about with this program to check your understanding:
Identify an iteration ending condition and a step decrement in the program.
The iteration ending condition is `counter = 0`.
The step decrement is -1.
State all the parts of a for loop statement.
Change the program so that it:
T minus...
12 ...
11 ...
10 ...
9 ...
Ignition sequence start.
8 ...
7 ...
6 ...
5 ...
4 ...
3 ...
2 ...
1 ...
0 ...
All engines running.
Lift off, we have a lift off on Artemis 1.
Tower clear.
Use the flowcharts if you need more support in understanding the steps required to meet the criteria. Use the Parsons code sorting exercise only if you are really stuck.
Run your code to check that your program has met the success criteria and produces the required output.
T minus...
12 ...
11 ...
10 ...
9 ...
Ignition sequence start.
8 ...
7 ...
6 ...
5 ...
4 ...
3 ...
2 ...
1 ...
0 ...
All engines running.
Lift off, we have a lift off on Artemis 1.
Tower clear.