1. Number chooser
1.1. Number chooser: simple version
from microbit import *
num = 5
display.show(num)
while True:
if button_a.is_pressed():
num += 1
if num > 9:
num = 0
display.show(num)
sleep(200)
1.2. Number chooser: by function
from microbit import *
def select_number():
counter = 5
display.show(counter)
while button_b.was_pressed() is False:
if button_a.is_pressed():
num += 1
if counter > 9:
counter = 0
display.show(counter)
sleep(200)
return counter
while True:
num = select_number()
display.scroll(num)
sleep(200)
Tasks
Add the parameters, min_num and max_num, to the select_number function so that the numbers are not limited to 0 to 9. Test with numbers 10 to 19. Modify the initial counter value to be the average of the min_num and max_num values. Add a short delay to display.show so the counter values are shown faster.
Add a start_num parameter to select_number, so it looks like: select_number(start_num, min_num, max_num).
Add the parameters, min_num and max_num, to the select_number function so that the numbers are not limited to 0 to 9. Test with numbers 10 to 19. Modify the initial counter value to be the average of the min_num and max_num values. Add a short delay to display.show so the counter values are shown faster.
from microbit import *
def select_number(min_num, max_num):
counter = int((min_num + max_num)/2)
display.show(counter, delay=200)
while button_b.was_pressed() is False:
if button_a.is_pressed():
counter += 1
if counter > max_num:
counter = min_num
display.show(counter, delay=200)
sleep(200)
return counter
while True:
num = select_number(10, 19)
display.scroll(num)
sleep(200)
Add a start_num parameter to select_number, so it looks like: select_number(start_num, min_num, max_num)
from microbit import *
def select_number(start_num, min_num, max_num):
counter = start_num
display.show(counter, delay=200)
while button_b.was_pressed() is False:
if button_a.is_pressed():
counter += 1
if counter > max_num:
counter = min_num
display.show(counter, delay=200)
sleep(200)
return counter
num = 14
while True:
num = select_number(num, 10, 19)
display.scroll(num)
sleep(200)