15. EXT: For loops using the range function
15.1. Looping Through a List of Values using element index from range function
- for i in range(len(value_list))
Iterates through all elements in
value_list, using i as the index.
value_list = ["D", "O", "G"] stores predefined values.for i in range(len(value_list)) loops over the list using index positions.from microbit import *
value_list = ["D", "O", "G"]
while True:
for i in range(len(value_list)):
display.scroll(i, delay=50)
display.scroll(value_list[i], delay=80)
sleep(500)
from microbit import *
y_list = [1, 4, 2, 3, 0] # max of 5 values, with each from 0 to 4
while True:
for x in range(len(y_list)):
y = y_list[x]
display.clear() # Clear previous pixel
display.set_pixel(x, y, 9) # Set pixel brightness to max
sleep(500)
Tasks
Modify the “DOG” example above to work for “STAR”.
Modify the pixel example to work for [2, 3, 2].
Modify the “DOG” example above to work for “STAR”.
from microbit import *
value_list = ["S", "T", "A", "R"]
while True:
for i in range(len(value_list)):
display.scroll(i, delay=50)
display.scroll(value_list[i], delay=80)
sleep(500)
Modify the pixel example to work for [2, 3, 2, 1].
from microbit import *
y_list = [2, 3, 2, 1] # max of 5 values, with each from 0 to 4
while True:
for x in range(len(y_list)):
y = y_list[x]
display.clear() # Clear previous pixel
display.set_pixel(x, y, 9) # Set pixel brightness to max
sleep(500)
15.2. Range function with start and stop values
- range(start_value, stop_value)
Returns a sequence of numbers, starting at the
start_valuenumber, and increments by 1 (by default), and ends before thestop_valuenumber.
from microbit import *
while True:
for n in range(2, 6):
display.scroll(n, delay=80)
sleep(500)
Tasks
Using the range function, write a for-loop that displays the numbers 3, 4, 5, 6, 7.
Using the range function, write a for-loop that displays the numbers from 4 up to but not including 9.
Using the range function, write a for-loop that displays the numbers 3, 4, 5, 6, 7.
from microbit import *
while True:
for n in range(3, 8):
display.scroll(n, delay=80)
sleep(500)
Using the range function, write a for-loop that displays the numbers from 4 up to but not including 9.
from microbit import *
while True:
for n in range(4, 9):
display.scroll(n, delay=80)
sleep(500)
15.3. Range function with a step size
- range(start_value, stop_value, step_size)
Returns a sequence of numbers, starting at the
start_valuenumber, incremented bystep_size, and ending before thestop_valuenumber.
from microbit import *
while True:
for n in range(1, 6, 2):
display.scroll(n, delay=50)
sleep(500)
Tasks
Using the range function, write a for-loop that displays the numbers 2, 4, 6, 8.
Using the range function, write a for-loop that displays the numbers 3, 7, 11, 15.
Using the range function, write a for-loop that displays the numbers 2, 4, 6, 8.
from microbit import *
while True:
for n in range(2, 9, 2):
display.scroll(n, delay=50)
sleep(500)
Using the range function, write a for-loop that displays the numbers 3, 7, 11, 15.
from microbit import *
while True:
for n in range(3, 16, 4):
display.scroll(n, delay=50)
sleep(500)
15.4. Using range to count down with a negative step size
from microbit import *
while True:
for n in range(10, 0, -1):
display.scroll(n, delay=80)
sleep(500)
Tasks
Using the range function, write a for-loop that displays the numbers 9, 7, 5, 3.
Using the range function, write a for-loop that displays the numbers 8, 5, 2.
Using the range function, write a for-loop that displays the numbers 9, 7, 5, 3.
from microbit import *
while True:
for n in range(9, 2, -2):
display.scroll(n, delay=80)
sleep(500)
Using the range function, write a for-loop that displays the numbers 8, 5, 2.
from microbit import *
while True:
for n in range(8, 1, -3):
display.scroll(n, delay=80)
sleep(500)