16. EXT: While loops
16.1. Events as conditions
from microbit import *
while True:
while button_a.is_pressed():
display.show(['I', Image.HEART, 'U'], delay=400)
sleep(300)
display.show(Image.ALL_CLOCKS, delay=50)
Tasks
Write a while-loop that displays a happy face while the A-button is pressed, and when it is not, Image.ALL_CLOCKS is used to display a rotating clock hand.
Write a while-loop that displays a sad face while the B-button is pressed, and when it is not, Image.ALL_CLOCKS is used to display a rotating clock hand.
Write a while-loop that displays a happy face while the A-button is pressed, and when it is not, Image.ALL_CLOCKS is used to display a rotating clock hand.
from microbit import *
while True:
while button_a.is_pressed():
display.show(Image.HAPPY)
sleep(300)
display.show(Image.ALL_CLOCKS, delay=50)
Write a while-loop that displays a sad face while the B-button is pressed, and when it is not, Image.ALL_CLOCKS is used to display a rotating clock hand.
from microbit import *
while True:
while button_b.is_pressed():
display.show(Image.SAD)
sleep(300)
display.show(Image.ALL_CLOCKS, delay=50)
16.2. Counters
16.3. Counting up
i
is the counter.i
starts off at 0 and is increased by 1 in the while-loop line: i += 1
.i += 1
is the same as i = i + 1
i < 10
, is True the while-loop runs.i += 1
causes i
to increase from 0 to 9.i
is 10 since i < 10
will be False when i = 10
.from microbit import *
while True:
i = 0
while i < 10:
display.scroll(i, delay=50)
i += 1
16.4. Counting down
i
starts off at 5 and is decreased by 1 in the while-loop line: i -= 1
.i -+= 1
is the same as i = i - 1
>
sign when counting down.i
is no longer greater than 1, i.e. when it is 1.from microbit import *
while True:
i = 5
while i > 1:
display.scroll(i, delay=50)
i -= 1
16.5. Step size
i += 2
sets a step size of 2.from microbit import *
while True:
i = 0
while i < 11:
display.scroll(i, delay=50)
i += 2
Tasks
Write a while-loop that counts up from 1 to 5, showing the numbers 1, 2, 3, 4, 5.
Write a while-loop that counts up from 3 to 12 in steps of 3, scrolling the numbers 3, 6, 9, 12.
Write a while-loop that counts down from 9 to 1, showing the numbers 9, 8, 7, 6, 5, 4, 3, 2, 1.
Write a while-loop that counts down from 24 to 18 in steps of 2, scrolling the numbers 24, 22, 20, 18.
Write 2 while loops to scroll 0 to 8 going up in 2s then 9 down to 1 going down in 2s.
Write a while-loop that counts up from 1 to 5, showing the numbers 1, 2, 3, 4, 5.
from microbit import *
while True:
i = 1
while i < 6:
display.scroll(i, delay=50)
i += 1
Write a while-loop that counts up from 3 to 12 in steps of 3, scrolling the numbers 3, 6, 9, 12.
from microbit import *
while True:
i = 3
while i < 13:
display.scroll(i, delay=50)
i += 3
Write a while-loop that counts down from 9 to 1, showing the numbers 9, 8, 7, 6, 5, 4, 3, 2, 1.
from microbit import *
while True:
i = 9
while i > 0:
display.scroll(i, delay=50)
i -= 1
Write a while-loop that counts down from 24 to 18 in steps of 2, scrolling the numbers 24, 22, 20, 18.
from microbit import *
while True:
i = 24
while i > 17:
display.scroll(i, delay=50)
i -= 2
Write 2 while loops to scroll 0 to 8 going up in 2s then 9 down to 1 going down in 2s.
from microbit import *
while True:
i = 0
while i < 9:
display.scroll(i, delay=50)
i += 2
i = 9
while i > 0:
display.scroll(i, delay=50)
i -= 2
sleep(1000)