Science Oxford Logo

Showing the day of the week

Finding the day of the week

The time function stores one extra piece of information we haven't used yet - what day of the week it is:

time['weekday']

If you ask the micro:bit to display this, it will give you a number - Monday is 1, Saturday is 6.


More if statements!

To ask the micro:bit to tell us the day of the week, we need to use more if statements:

if time['weekday'] == '6':
    display.scroll('Today is Saturday')

This asks, is the weekday 6? If that is true, then tell us that today is Saturday.

You can expand this with all of the days of the week. For example:

if button_b.was_pressed():
    time = get_time()
    if time['weekday'] == '6':
        display.scroll('Today is Saturday')
    if time['weekday'] == '7':
        display.scroll('Today is Sunday')

Decide when you want the micro:bit to tell you the day of the week (in the above code, we used when the Button B is pressed), then finish writing the if statements.


A full version of this code is below for you to add to your program, modify, and test.


display.show(Image.DUCK)
sleep(1000)

while True:
    if button_a.is_pressed():
        time = get_time()
        display.scroll(time['year'])

    if button_b.was_pressed():
        time = get_time()
        if time['weekday'] == '1':
            display.scroll('Today is Monday')
        if time['weekday'] == '2':
            display.scroll('Today is Tuesday')
        if time['weekday'] == '3':
            display.scroll('Today is Wednesday')

    else:
        display.clear()
        sleep(100)

Download the example here.