13. EXT: for-loops
13.1. Spacing characters
from microbit import *
card_name = 'ace'
string_length = len(card_name)
spacing_character = "*"
while True:
for i in range(string_length):
display.scroll(card_name[i])
if i < string_length - 1:
display.scroll(spacing_character)
sleep(300)
Tasks
Write a for-loop to scroll each letter in ‘bot’ individually with a ‘|’ 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 ‘bot’ individually with an ‘|’ between them.
from microbit import *
string = 'bot'
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)
13.2. Code shifts
- ord(character)
- returns the ASCII value for character.e.g
display.scroll(ord('a'))scrolls 97.
- chr(number)
- returns the character for the ASCII number.e.g
display.scroll(chr(97))scrolls ‘a’.
ord(character) converts the character to its ASCII number.ascii_num -= 2 decreases the ASCII number by 2.chr(ascii_num) converts the modified ASCII number back to a character.from microbit import *
code_string = 'hqz'
while True:
for character in code_string:
ascii_num = ord(character)
ascii_num -=2
new_char = chr(ascii_num)
display.scroll(new_char, delay=50)
sleep(300)
Tasks
Write a for-loop that converts the code word “ald”, then scrolls the converted secret word “dog”.
Write a for-loop that converts the code word “ald”, then scrolls the converted secret word “dog”.
from microbit import *
code_string = 'ald'
while True:
for character in code_string:
ascii_num = ord(character)
ascii_num +-=3
new_char = chr(ascii_num)
display.scroll(new_char, delay=50)
sleep(300)
13.3. Prime factors
- dividend % divisor
- returns the remainder when dividend is divided by divisor.e.g 14 % 3 returns 2, because 14 divided by 3 is 4 with a remainder of 2.
5 % 2 returns the remainder 1.test_num % num returns the remainder from division.if test_num % num == 0 checks whether the remainder is zero or not.from microbit import *
primes = [2, 3, 5]
test_num = 15
while True:
for num in primes:
if test_num % num == 0:
display.scroll(num, delay=50)
sleep(300)
Tasks
Modify the code above to find the prime factors of 42.
Modify the code above to find the prime factors of 42.
from microbit import *
primes = [2, 3, 5, 7]
test_num = 42
while True:
for num in primes:
if test_num % num == 0:
display.scroll(num, delay=50)
sleep(300)
13.4. Abbreviations
- string[start: stop]
- returns character start up to but not including character stop of the string.
'January'[0:3]returns characters 0 to 2 which is ‘Jan’.
month[0:3].from microbit import *
months = ['January', 'February', 'March']
while True:
for month in months:
short_month = month[0:3]
display.scroll(short_month, delay=80)
sleep(300)
Tasks
Modify the code above to scroll the first 3 letters of the days: “Monday”, “Tuesday”, “Wednesday”.
Modify the code above to scroll the first 3 letters of the days: “Monday”, “Tuesday”, “Wednesday”.
from microbit import *
days = ["Monday", "Tuesday", "Wednesday"]
while True:
for day in days:
short_day = day[0:3]
display.scroll(short_day, delay=80)
sleep(300)
13.5. Vowels
in, can be used to test whether a character is in a string.- element in collection
- returns True if element is found in collection, otherwise False.
"A" in "AEIOU"returns True.
- element not in collection
- returns True if element is not found in collection, otherwise False.
"B" not in "AEIOU"returns True.
name[0] in "AEIOU" returns True if character 0 in the name string is in the string of vowels.name[0] not in "AEIOU" returns True if character 0 in the name string is not in the string of vowels.- string.upper()
- returns a new string with all characters converted to uppercase.
"hello world".upper()returns “HELLO WORLD”.e.g.anna.upper()returns “ANNA”
from microbit import *
name_list = ['Alexia', 'Bethany', 'Chloe']
while True:
for name in name_list:
if name[0] in "AEIOU":
display.scroll(name.upper(), delay=50)
sleep(300)
The code below scrolls the name in upper case only if the name does not start with a vowel.
from microbit import *
name_list = ['Alexia', 'Bethany', 'Chloe']
while True:
for name in name_list:
if name[0] not in "AEIOU":
display.scroll(name.upper(), delay=50)
sleep(300)
- sorted_list = sorted(original_list)
- returns a new list with the elements of original_list sorted in ascending order by default.sorted_name_list = sorted([“Charlie”, “Alice”, “Bob”]) returns the list sorted_name_list which is [“Alice”, “Bob”, “Charlie”].
The code below sorts the list first then scrolls the name in uppercase.
from microbit import *
name_list = ['Zoe', 'Yasmin', 'Xena']
sorted_name_list = sorted(name_list)
while True:
for name in sorted_name_list:
display.scroll(name.upper(), delay=50)
sleep(300)
Tasks
Write a for-loop to scroll names beginning with a vowel in [‘Gabriella’, ‘Julia’, ‘Isabel’, ‘Hannah’, ‘Emily’, ‘Fiona’, ‘Chloe’, ‘Daisy’, ‘Anna’, ‘Bella’]. Display the names in uppercase. Display the names in alphabetical order.
Write a for-loop to scroll names beginning with a vowel in [‘Gabriella’, ‘Julia’, ‘Isabel’, ‘Hannah’, ‘Emily’, ‘Fiona’, ‘Chloe’, ‘Daisy’, ‘Anna’, ‘Bella’]. Display the names in uppercase. Display the names in alphabetical order.
from microbit import *
name_list = ['Gabriella', 'Julia', 'Isabel', 'Hannah', 'Emily', 'Fiona', 'Chloe', 'Daisy', 'Anna', 'Bella']
sorted_name_list = sorted(name_list)
while True:
for name in sorted_name_list:
if name[0] in "AEIOU":
display.scroll(name.upper(), delay=50)
sleep(300)
13.6. Storing values in a list
Exercises
Here is some fun code that displays a heart image as a series of 3 pixels. Change it from 3 to 5 pixels.
Here is some fun code that displays a heart image as a series of shown pixels. Change the pop parameter to pop the last item via -1 to clear the image in reverse.
Here is some fun code that displays a heart image as a series of 3 pixels. Change it from 3 to 5 pixels.
from microbit import *
heart = [
[0, 1, 1, 1, 0],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[0, 1, 1, 1, 0],
[0, 0, 1, 0, 0]
]
lit_pixels = [] # Keep track of three active pixels
while True:
for y in range(5):
for x in range(5):
if heart[y][x]: # Only process active parts of the heart shape
display.set_pixel(x, y, 9)
lit_pixels.append((x, y)) # Store pixel coordinates
# If more than 3 pixels are lit, remove the oldest one
if len(lit_pixels) > 3:
old_x, old_y = lit_pixels.pop(0)
display.set_pixel(old_x, old_y, 0)
sleep(100)
sleep(500)
display.clear()
sleep(500)
Here is some fun code that displays a heart image as a series of shown pixels. Change the pop parameter to pop the last item via -1 to clear the image in reverse.
from microbit import *
heart = [
[0, 1, 1, 1, 0],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[0, 1, 1, 1, 0],
[0, 0, 1, 0, 0]
]
lit_pixels = [] # Keep track of active pixels
while True:
# Light up pixels one by one
for y in range(5):
for x in range(5):
if heart[y][x]: # Only process active parts of the heart shape
display.set_pixel(x, y, 9)
lit_pixels.append((x, y)) # Store pixel coordinates
sleep(100)
# Fade out pixels one by one instead of clearing all at once
while lit_pixels:
old_x, old_y = lit_pixels.pop(0)
display.set_pixel(old_x, old_y, 0)
sleep(100)
sleep(500)
Here is some fun code that displays a heart image as a series of shown pixels, then clears it by choosing random pixels from the stored list of pixels. Modify it to dim the pixels instead of turning them off.
from microbit import *
import random # Import random module
heart = [
[0, 1, 1, 1, 0],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[0, 1, 1, 1, 0],
[0, 0, 1, 0, 0]
]
lit_pixels = [] # Keep track of active pixels
while True:
# Light up pixels one by one
for y in range(5):
for x in range(5):
if heart[y][x]: # Only process active parts of the heart shape
display.set_pixel(x, y, 9)
lit_pixels.append((x, y)) # Store pixel coordinates
sleep(100)
# Remove pixels randomly instead of sequentially
while lit_pixels:
random_index = random.randint(0, len(lit_pixels) - 1) # Get a random pixel index
old_x, old_y = lit_pixels.pop(random_index) # Remove a random pixel
display.set_pixel(old_x, old_y, 0)
sleep(50)
sleep(500)