12. For loops
12.1. For loops with strings
for welcome_character in welcome_string
takes each character in the string welcome_string
and puts it in the variable welcome_character
.display.scroll(welcome_character)
then scrolls the character.from microbit import *
welcome_string = 'Hello'
while True:
for welcome_character in welcome_string:
display.scroll(welcome_character)
sleep(300)
Tasks
Write a for-loop to scroll each letter in ‘winner’ individually.
Write a for-loop to scroll each digit in ‘2023’ individually.
Write a for-loop to scroll each letter in ‘winner’ individually.
from microbit import *
string = 'winner'
while True:
for character in string:
display.scroll(character)
sleep(300)
Write a for-loop to scroll each digit in ‘1966’ individually.
from microbit import *
string = '1966'
while True:
for character in string:
display.scroll(character)
sleep(300)
12.2. Add actions to a for-loop
from microbit import *
welcome_string = 'Hello'
spacing_character = "_"
while True:
for welcome_character in welcome_string:
display.scroll(welcome_character)
display.scroll(spacing_character)
sleep(300)
Tasks
Write a for-loop to scroll each letter in ‘ace’ individually with an ‘*’ between them.
Write a for-loop to scroll each digit in ‘8850’ individually with a ‘-’ between them.
Write a for-loop to scroll each letter in ‘ace’ individually with an ‘*’ between them.
from microbit import *
string = 'ace'
spacing_character = "*"
while True:
for character in string:
display.scroll(character)
display.scroll(spacing_character)
sleep(300)
Write a for-loop to scroll each digit in ‘8850’ individually with a ‘-’ between them.
from microbit import *
string = '2023'
spacing_character = "-"
while True:
for character in string:
display.scroll(character)
display.scroll(spacing_character)
sleep(300)
12.3. For loops with lists
from microbit import *
wise_men = ['Melchior', 'Caspar', 'Balthazar']
while True:
for wise_man in wise_men:
display.scroll(wise_man, delay=80)
sleep(300)
from microbit import *
primes = [2, 3, 5, 7]
while True:
for num in primes:
display.show(num)
sleep(300)
Tasks
Write a for-loop to scroll each name in the list
['Bugs', 'Daffy', 'Marvin']
.Write a for-loop to scroll each number in the list
[1, 2, 3, 5, 8]
.
Write a for-loop to scroll each name in the list ['Bugs', 'Daffy', 'Marvin']
.
from microbit import *
names_list = ['Bugs', 'Daffy', 'Marvin']
while True:
for name in names_list:
display.scroll(name)
sleep(300)
Write a for-loop to scroll each number in the list [1, 2, 3, 5, 8]
.
from microbit import *
num_list = [1, 2, 3, 5, 8]
while True:
for num in num_list:
display.scroll(num)
sleep(300)
12.4. Nested For loops
from microbit import *
col_letters = ['A', 'B', 'C']
row_nums = ['1', '2', '3', '4']
while True:
for col in col_letters:
for row in row_nums:
display.scroll(col + row, delay=200)
for col in col_letters
takes each element in the list col_letters
and puts it in the variable col
for use in the loop.for row in row_nums
takes each element in the list row_nums
and puts it in the variable row
for use in the loop.for col in col_letters
, runs 3 times since there are 3 elements in ['A', 'B', 'C']
.for row in row_nums
, runs 4 times since there are 4 elements in ['1', '2', '3', '4']
.+
in col + row
does a text join. When col
= ‘A’ and row
= ‘1’, col + row
will result in 'A1'
.from microbit import *
nums_1_list = [5, 6]
nums_2_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
while True:
for num_1 in nums_1_list:
for num_2 in nums_2_list:
display.scroll(num_1 * num_2, delay=80)
Tasks
Write a for-loop that shows the result from multiplying each number in the list,
[3, 5, 7]
by 5, using a variable for each part of the multiplication.Write a nested for-loop that finds the sum of every different combination of two numbers from the two lists:
[2, 4, 6]
and[3, 5, 7]
.
Write a for-loop that shows the result from multiplying each number in the list, [3, 5, 7]
by 5, using a variable for each part of the multiplication.
from microbit import *
nums_1_list = [3, 5, 7]
num_2 = 5
while True:
for num_1 in nums_1_list:
display.scroll(num_1 * num_2, delay=80)
Write a nested for-loop that finds the sum of every different combination of two numbers from the two lists: [2, 4, 6]
and [3, 5, 7]
.
from microbit import *
nums_1_list = [2, 4, 6]
nums_2_list = [3, 5, 7]
while True:
for num_1 in nums_1_list:
for num_2 in nums_2_list:
display.scroll(num_1 + num_2, delay=80)
12.5. For loops with mixed lists
from microbit import *
mixed_list = ['I', Image.HEART, 3.14]
while True:
for element in mixed_list:
display.show(element, delay=200)
sleep(700)
sleep(1000)
Tasks
Create a mixed list to display the message to be asleep at 10 o’clock.
Create a mixed list to display you’re 3 favourite animals with their number order.
Create a mixed list to display the message to be asleep at 10 o’clock.
from microbit import *
mixed_list = [Image.ASLEEP, '@', Image.CLOCK10]
while True:
for element in mixed_list:
display.show(element, delay=200)
sleep(700)
sleep(1000)
Create a mixed list to display you’re 3 favourite animals in number order.
from microbit import *
mixed_list = ['#1', Image.RABBIT, '#2', Image.DUCK, '#3', Image.TORTOISE]
while True:
for element in mixed_list:
display.show(element, delay=200)
sleep(700)
sleep(1000)