14. For loops using the range function
See: https://www.w3schools.com/python/python_for_loops.asp
14.1. Range function starting at 0
- range(stop_value)
Returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends before the
stop_value
number.
from microbit import *
while True:
for n in range(3):
display.scroll(n, delay=80)
sleep(500)
Tasks
Using the range function, write a for-loop that displays the numbers from 0 to 5.
Using the range function, write a for-loop that displays the numbers from 0 up to but not including 10.
Using the range function, write a for-loop that displays the numbers from 0 to 5.
from microbit import *
while True:
for n in range(6):
display.scroll(n, delay=80)
sleep(500)
Using the range function, write a for-loop that displays the numbers from 0 up to but not including 10.
from microbit import *
while True:
for n in range(10):
display.scroll(n, delay=80)
sleep(500)
14.2. Range function with start and stop values
- range(start_value, stop_value)
Returns a sequence of numbers, starting at the
start_value
number, and increments by 1 (by default), and ends before thestop_value
number.
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)
14.3. Range function with a step size
- range(start_value, stop_value, step_size)
Returns a sequence of numbers, starting at the
start_value
number, incremented bystep_size
, and ending before thestop_value
number.
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)
14.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)