4. Words
4.1. Chr Syntax
- chr(number)
- The chr() function returns the character that represents the specified unicode number.Standard ASCII values can be used as well.
4.2. Producing character lists via list comprehension
char_nos = [i for i in range(65, 91)]
, makes a list of the ASCII code numbers for the upper case letters from 65 to 90.chars = [chr(i) for i in char_nos]
, takes that list of numbers and use the chr function to get the corresponding character for that ASCII number.from microbit import *
char_nos = [i for i in range(65, 91)] # upper case letters
chars = [chr(i) for i in char_nos]
print(chars)
Tasks
Modify the code to produce a list of the lower case letters.
Modify the code to produce a list of the digits.
Modify the code to produce a list of the common punctuation.
Modify the code to produce a list of the lower case letters.
from microbit import *
char_nos = [i for i in range(97, 123)] # lower case letters
chars = [chr(i) for i in char_nos]
print(chars)
[“a”, “b”, “c”, “d”, “e”, “f”, “g”, “h”, “i”, “j”, “k”, “l”, “m”, “n”, “o”, “p”, “q”, “r”, “s”, “t”, “u”, “v”, “w”, “x”, “y”, “z”]
Modify the code to produce a list of the digits.
from microbit import *
char_nos = [i for i in range(48, 58)] # digits
chars = [chr(i) for i in char_nos]
print(chars)
[“0”, “1”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”]
Modify the code to produce a list of the common punctuation.
from microbit import *
char_nos = [i for i in range(32, 48)] # punctuation and symbols
chars = [chr(i) for i in char_nos]
print(chars)
[” “, “!”, ‘”’, “#”, “$”, “%”, “&”, “’”, “(”, “)”, “*”, “+”, “,”, “-”, “.”, “/”]
4.3. Code design
a = button_a.was_pressed()
clears the A-button presses so it can be checked again in the other functions.get_string()
calls get_char()
to add characters to the word string as long as the B-button hasn’t been pressed, otherwise it returns the word string, user_text.from microbit import *
chars = []
max_char_index = ............
middle_index = ..............
def get_char():
current = middle_index
display.show(chars[........])
# the while loops runs until button-A is pressed
while button_a.was_pressed() is False:
# pressing B doesn't add a character but returns back to get_string
if button_b.is_pressed():
return ""
if accelerometer.get_x() > 300:
current .......
elif accelerometer.get_x() < -300:
current .......
current = max(0, min(current, max_char_index))
display.show(chars[.......])
sleep(330)
# button-A was pressed so return chosen character
return chars[........]
def get_string():
user_text = ""
# continue adding characters if B-button has not been pressed
while button_b.was_pressed() is False:
user_text ...... get_char()
# B-button was pressed, return final word so it can be scrolled
return user_text
while True:
display.show(Image.ARROW_W)
# press A to start
if button_a.is_pressed():
display.clear()
sleep(1000)
# clear the A-button pressing so it can be checked for being pressed again in get_string
a = button_a.was_pressed()
currentWord = ...........()
display.scroll(...........)
sleep(330)
Tasks
Create the code.
from microbit import *
chars = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
max_char_index = len(chars) - 1
middle_index = int(max_char_index/2)
def get_char():
current = middle_index
display.show(chars[current])
# the while loops runs until button-A is pressed
while button_a.was_pressed() is False:
# pressing B doesn't add a character but returns back to get_string
if button_b.is_pressed():
return ""
if accelerometer.get_x() > 300:
current += 1
elif accelerometer.get_x() < -300:
current -= 1
current = max(0, min(current, max_char_index))
display.show(chars[current])
sleep(330)
# button-A was pressed so return chosen character
return chars[current]
def get_string():
user_text = ""
# continue adding characters if B-button has not been pressed
while button_b.was_pressed() is False:
user_text += get_char()
# B-button was pressed, return final word so it can be scrolled
return user_text
while True:
display.show(Image.ARROW_W)
# press A to start
if button_a.is_pressed():
display.clear()
sleep(1000)
# clear the A-button pressing so it can be checked for being pressed again in get_string
a = button_a.was_pressed()
currentWord = get_string()
display.scroll(currentWord)
sleep(330)
Tasks
Modify the code to use lower case letters.
Modify the code to use numbers instead of letters.
Modify the code to add tilting in the y direction to be able to choose the vowels directly.
Modify the code to use tilting in the x direction for uppercase; tilting in the y direction for lowercase.
Modify the code to use lower case letters.
from microbit import *
chars = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
"m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"
]
max_char_index = len(chars) - 1
middle_index = int(max_char_index/2)
def get_char():
current = middle_index
display.show(chars[current])
# the while loops runs until button-A is pressed
while button_a.was_pressed() is False:
# pressing B doesn't add a letter but returns back to
if button_b.is_pressed():
return ""
if accelerometer.get_x() > 300:
current += 1
elif accelerometer.get_x() < -300:
current -= 1
current = max(0, min(current, max_char_index))
display.show(chars[current])
sleep(330)
# button-A was pressed so return chosen letter
return chars[current]
def get_string():
user_text = ""
# continue adding letters if B-button has not been pressed
while button_b.was_pressed() is False:
user_text += get_char()
# B-button was pressed, return final word so it can be scrolled
return user_text
while True:
display.show(Image.ARROW_W)
# press A to start
if button_a.is_pressed():
display.clear()
sleep(1000)
# clear the A-button pressing so it can be checked for being pressed again in get_string
a = button_a.was_pressed()
currentWord = get_string()
display.scroll(currentWord)
sleep(330)
Modify the code to use numbers instead of letters.
from microbit import *
chars = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
max_char_index = len(chars) - 1
middle_index = int(max_char_index / 2)
def get_char():
current = middle_index
display.show(chars[current])
# the while loops runs until button-A is pressed
while button_a.was_pressed() is False:
# pressing B doesn't add a letter but returns back to
if button_b.is_pressed():
return ""
if accelerometer.get_x() > 300:
current += 1
elif accelerometer.get_x() < -300:
current -= 1
current = max(0, min(current, max_char_index))
display.show(chars[current])
sleep(330)
# button-A was pressed so return chosen letter
return chars[current]
def get_string():
user_text = ""
# continue adding letters if B-button has not been pressed
while button_b.was_pressed() is False:
user_text += get_char()
# B-button was pressed, return final word so it can be scrolled
return user_text
while True:
display.show(Image.ARROW_W)
# press A to start
if button_a.is_pressed():
display.clear()
sleep(1000)
# clear the A-button pressing for checking again in get_string
a = button_a.was_pressed()
currentWord = get_string()
display.scroll(currentWord)
sleep(330)
Modify the code to add tilting in the y direction to be able to choose the vowels directly.
from microbit import *
chars = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
max_char_index = len(chars) - 1
middle_index = int(max_char_index / 2)
vowels = ["A", "E", "I", "O", "U"]
def get_char():
current = middle_index
vowel_current = 2
# flag 0 for current and 1 for vowel_current
change_flag = 0
while button_a.was_pressed() is False:
if button_b.is_pressed():
return ""
if accelerometer.get_y() > 300:
vowel_current += 1
change_flag = 1
elif accelerometer.get_y() < -300:
vowel_current -= 1
change_flag = 1
elif accelerometer.get_x() > 300:
current += 1
change_flag = 0
elif accelerometer.get_x() < -300:
current -= 1
change_flag = 0
if change_flag == 0:
current = max(0, min(current, max_char_index))
display.show(chars[current])
else:
vowel_current = max(0, min(vowel_current, 4))
display.show(vowels[vowel_current])
sleep(330)
if change_flag == 0:
return chars[current]
else:
return vowels[vowel_current]
def get_string():
user_text = ""
while button_b.was_pressed() is False:
user_text += get_char()
return user_text
while True:
display.show(Image.ARROW_W)
if button_a.is_pressed():
display.clear()
sleep(1000)
a = button_a.was_pressed()
currentWord = get_string()
display.scroll(currentWord)
sleep(330)
Modify the code to use tilting in the x direction for uppercase; tilting in the y direction for lowercase.
from microbit import *
chars = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
"O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"]
max_char_index = len(chars) - 1
middle_index = int(max_char_index / 2)
chars2 = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
"o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
max_char_index2 = len(chars2) - 1
middle_index2 = int(max_char_index2 / 2)
def get_char():
current = middle_index
current2 = middle_index2
# flag 0 for current and 1 for current2
change_flag = 0
while button_a.was_pressed() is False:
if button_b.is_pressed():
return ""
if accelerometer.get_y() > 300:
current2 += 1
change_flag = 1
elif accelerometer.get_y() < -300:
current2 -= 1
change_flag = 1
elif accelerometer.get_x() > 300:
current += 1
change_flag = 0
elif accelerometer.get_x() < -300:
current -= 1
change_flag = 0
if change_flag == 0:
current = max(0, min(current, max_char_index))
display.show(chars[current])
else:
current2 = max(0, min(current2, max_char_index2))
display.show(chars2[current2])
sleep(330)
if change_flag == 0:
return chars[current]
else:
return chars2[current2]
def get_string():
user_text = ""
while button_b.was_pressed() is False:
user_text += get_char()
return user_text
while True:
display.show(Image.ARROW_W)
if button_a.is_pressed():
display.clear()
sleep(1000)
a = button_a.was_pressed()
currentWord = get_string()
display.scroll(currentWord)
sleep(330)