14. EXT: for-loops
14.1. Using if to test strings within for-loops
name[0] in "AEIOU"
returns True if the first character in the name string is in hte string of vowels..upper()
.anna.upper()
returns “ANNA”The code below scrolls the name in upper case only if the first letter is a vowel.
from microbit import *
name = "Anna"
while True:
if name[0] in "AEIOU":
display.scroll(name.upper(), delay=50)
Tasks
Write a for-loop to scroll names beginning with a vowel in [‘Olivia’, ‘Emily’, ‘Chloe’, ‘Catherine’, ‘Anna’, ‘Gabriella’, ‘Hannah’, ‘Isabel’, ‘Julia’]. Display the names in uppercase.
Write a for-loop to scroll names beginning with a vowel in [‘Olivia’, ‘Emily’, ‘Chloe’, ‘Catherine’, ‘Anna’, ‘Gabriella’, ‘Hannah’, ‘Isabel’, ‘Julia’]. Display the names in uppercase.
from microbit import *
name_list = ['Olivia', 'Emily', 'Chloe', 'Catherine', 'Anna', 'Gabriella', 'Hannah', 'Isabel', 'Julia']
while True:
for name in name_list:
if name[0] in "AEIOU":
display.scroll(name.upper(), delay=50)
sleep(300)
14.2. 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)
14.3. 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 = [7, 9]
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 nested for-loop that finds the sum of every different combination of one number from each of the two lists:
[1, 2, 3]
and[6, 5, 4]
.Write a nested for-loop that scrolls the 2 digit number formed from the joining of every different combination of one number from each of the two lists:
[1, 2, 3]
and[4, 5]
, keeping the digit from the first list first.
Write a nested for-loop that scrolls the sum of every different combination of one number from each of the two lists: [1, 2, 3]
and [6, 5, 4]
.
from microbit import *
nums_1_list = [1, 2, 3]
nums_2_list = [6, 5, 4]
while True:
for num_1 in nums_1_list:
for num_2 in nums_2_list:
display.scroll(num_1 + num_2, delay=50)
Write a nested for-loop that scrolls the 2 digit number formed from the joining of every different combination of one number from each of the two lists: [1, 2, 3]
and [4, 5]
, keeping the digit from the first list first.
from microbit import *
nums_1_list = [1, 2, 3]
nums_2_list = [4, 5]
while True:
for num_1 in nums_1_list:
for num_2 in nums_2_list:
display.scroll(str(num_1) + str(num_2), delay=50)