5. For Loops Code Ordering
5.1. Question 1
A student wants to code an electronic laser fence alarm system.
Rearrange the lines below to create a loop that cycles through each individual alert code letter inside the alarm state code string
"ALERT" as part of his system’s security protocol.Drag and drop lines into the correct order and click to adjust indentation:
☰
☰
display.show(code_letter)
☰
from microbit import *
☰
sleep(200)
☰
for code_letter in "ALERT":
5.2. Question 2
Put the code snippets in order to build an automated submarine sonar tracking scan.
The program must cycle through a custom sequence of target depth values (100, 200, 300) and flash each numeric depth value onto the micro:bit display screen panel.
Drag and drop lines into the correct order and click to adjust indentation:
☰
from microbit import *
☰
display.scroll(meters)
☰
for meters in depth_targets:
☰
☰
depth_targets = [100, 200, 300]
5.3. Question 3
A student wants to flash a secret password symbol by symbol on the screen.
Order the lines to step through a sequence of single characters stored in a variable named
secret_code to show them one by one with a dash between them.Drag and drop lines into the correct order and click to adjust indentation:
☰
display.show("-")
☰
while True:
☰
for symbol in secret_code:
☰
display.show(symbol)
☰
from microbit import *
☰
secret_code = "SOS"
☰
☰
sleep(400)
☰
sleep(400)
5.4. Question 4
Put the code lines in order to build a beacon that cycles through three warning status words (LOW, MED, HIGH) stored in a sequence list.
It should scroll each status word completely across the display panel before moving to the next.
Drag and drop lines into the correct order and click to adjust indentation:
☰
warning_levels = ["LOW", "MED", "HIGH"]
☰
from microbit import *
☰
sleep(500)
☰
display.scroll(status)
☰
☰
for status in warning_levels:
5.5. Question 5
Rearrange the lines to create a custom scoreboard display for a mini-game.
The loop needs to look at a sequence of target scores (10, 25, 50) and show each milestone number on the display panel with a short pause between them.
Drag and drop lines into the correct order and click to adjust indentation:
☰
for target_score in score_milestones:
☰
☰
from microbit import *
☰
sleep(600)
☰
score_milestones = [10, 25, 50]
☰
display.scroll(target_score)