14. For loops using the range function

See: `<https://www.w3schools.com/python/python_for_loops.asp

To loop through a set of code a specified number of times, use the range() function.
The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and ends at a specified number.

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.

range(3) returns the numbers 0, 1, 2. It starts at 0. It goes up by 1. It stops before 3, at 2.
The code below will display the numbers from 0 to 2.
from microbit import *

while True:
    for n in range(3):
        display.scroll(n, delay=80)
    sleep(500)

Tasks

  1. Using the range function, write a for-loop that displays the numbers from 0 to 3.

  2. Using the range function, write a for-loop that displays the numbers from 0 up to but not including 5.

Using the range function, write a for-loop that displays the numbers from 0 to 3.

from microbit import *

while True:
    for n in range(4):
        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 5.

from microbit import *

while True:
    for n in range(5):
        display.scroll(n, delay=80)
    sleep(500)

14.2. Adding to the range function variable

The code below uses the range function to get the numbers from 0 to 2 then adds 1 to each and scrolls 1 to 3.
from microbit import *

while True:
    for n in range(3):
        display.scroll(n + 1, delay=80)
    sleep(500)

Tasks

  1. Using the range function, write a for-loop that displays the numbers from 1 to 5.

  2. Using the range function, write a for-loop that displays the numbers from 2 to 5.

Using the range function, write a for-loop that displays the numbers from 1 to 5.

from microbit import *

while True:
    for n in range(5):
        display.scroll(n + 1, delay=80)
    sleep(500)

Using the range function, write a for-loop that displays the numbers from 2 to 5.

from microbit import *

while True:
    for n in range(4):
        display.scroll(n + 2, delay=80)
    sleep(500)

14.3. Subtracting from the range function variable

The code below uses the range function to get the numbers from 0 to 3 then subtracts 1 from each and scrolls -1 to 2.
from microbit import *

while True:
    for n in range(4):
        display.scroll(n - 1, delay=80)
    sleep(500)

Tasks

  1. Using the range function, write a for-loop that displays the numbers from -1 to 1.

  2. Using the range function, write a for-loop that displays the numbers from -2 to 2.

Using the range function, write a for-loop that displays the numbers from -1 to 1.

from microbit import *

while True:
    for n in range(3):
        display.scroll(n - 1, delay=80)
    sleep(500)

Using the range function, write a for-loop that displays the numbers from -2 to 2.

from microbit import *

while True:
    for n in range(5):
        display.scroll(n - 2, delay=80)
    sleep(500)

14.4. Multiplying the range function variable

The code below uses the range function to get the numbers from 0 to 2 then doubles each and scrolls 0 to 4.
from microbit import *

while True:
    for n in range(3):
        display.scroll(n * 2, delay=80)
    sleep(500)

Tasks

  1. Using the range function, write a for-loop that displays the numbers 0, 3, 6, 9.

  2. Using the range function, write a for-loop that displays the numbers 0, -1, -2, -3.

Using the range function, write a for-loop that displays the numbers 0, 3, 6, 9.

from microbit import *

while True:
    for n in range(4):
        display.scroll(n * 3, delay=80)
    sleep(500)

Using the range function, write a for-loop that displays the numbers 0, -1, -2, -3.

from microbit import *

while True:
    for n in range(4):
        display.scroll(n * -1, delay=80)
    sleep(500)