Curious Conditionals

A Mu Direction

Learn It

  • We've used the browser-based IDE for our Micro:bit, but it's a little nicer working in a proper application when we need to find problems with our code. We're going to use a program called Mu from this point forward. You can use this method, or the web browser version from last lesson with your Micro:bit at home.
  • NOTE: You can download Mu for use at home here (All operating systems).
  • Start by loading Mu from the Open drive, in this folder:
  • G:\Computing 2015-2016\Y7\BBC MicroBit

Learn It

  • Previously, we looked at how using variables can allow our Micro:bit programs to be more flexible. We'll look today at how using If commands and some variables can make our programs more powerful.
  • If allows you to check if a particular statement is true, then carries out some instructions and then moves to the next instructions outside the If block.
  • We can also add Elif and Else commands with a separate set of instructions to tell the computer what to do in different situations. The pseudocode below gives an example of an If block:
hungerLevel = input("From 0-10, how hungry are you? ")

IF hungerLevel == 10:
    PRINT "Eat a proper dinner"
ELIF hungerLevel > 5:
    PRINT "Eat a sandwich"
ELIF hungerLevel > 2 :
    PRINT "Eat an apple"
    PRINT "Wait until later"

PRINT "Thank you for using the food guide."
  • If the user enters 10, the first If statement is true, so the user will be told to eat a proper dinner. The program then leaves the If block (Shown by writing END IF here) and the user is thanked for using the app.
  • If they'd entered 9, a sandwich is recommended, then they're thanked.
  • Entering 5 would advise the user to eat an apple, then they're thanked. Why is that?
  • A user who who chooses 1, -5 or 0 would be advised to wait until later, and is then thanked. This is because none of the other statements in the If block were true.
  • If you're still unsure, this video tutorial gives another example, using Scratch:

Learn It

  • Let's write a program to make the Micro:bit work as a simple torch. We can write some simple pseudocode to express what we want to achieve:
    IF button A is pushed:
        Turn all LEDs on
    ELSE IF button B is pushed:
        Turn all LEDs off
        DO NOTHING
    END IF
  • By using IF statements in this way, the LEDs instructions are only carried out when the appropriate button is pushed.
  • Let's code it! Clear the example code,then paste this into the Mu window.
from microbit import *

while True:
    if button_a.is_pressed():
    elif button_b.is_pressed():
  • Click the Flash button at the top of the window to push your program to the Micro:bit.
  • Did you notice that we didn't add an else line to match the one in our pseudocode? As we don't want to take any action if a button isn't pushed, we don't need to write any code.
  • Challenge: Can you remix the code above to write a happy/sad program? If the user pushes button A, the HAPPY image should be loaded onto the screen. If they push B, SAD should be shown. If they push nothing, the image should change to ASLEEP.

Learn It

  • Combining If with variables allows us to start to make more useful programs.
  • We could make a simple counting app to help a toddler learn their first few numbers. Starting at zero, the buttons could be used to show the digits from 0-9 o the display.
  • Our pseudocode could be something like:

    IF buttonA pushed:
        DECREASE currentNumber BY 1
        PRINT currentNumber
    ELSE IF buttonB pushed:
        INCREASE currentNumber BY 1
        PRINT currentNumber

Code It

  • Copy this code to your Micro:bit…
from microbit import *
currentNumber = 0

while True:
    if button_a.is_pressed():
	currentNumber -= 1
    elif button_b.is_pressed():
	currentNumber += 1
  • Question: Why is the currentNumber = 0 line sat above the While True: line? What would happen if it was inside the WHILE loop?
  • With this code working, could we improve it?
  • I only want the program to show the numbers 0-9, but at the moment it'll give me negative numbers, and will count beyond 9.
  • Programmers can add If statements inside other if statements (we call this nesting) to produce even more powerful programs. We just need to remember to tab in the code correctly to tell the computer which commands are in our If block and which aren't.
  • If you're finding the concepts tricky, the video tutorial below will walk you through what you need to know:

Badge It

  • Silver: Modify the code above to start at 6, and show the 6 times table forwards and backwards. Upload your code to
  • Gold: Change the code back to a counting up and down (one at a time) program, but modify the code so that the user can't make the numbers go above 9 or below 0.
  • You should be able to do this with two new lines of code. This might give you a step in the right direction:
if currentNumber >= 1:
  • Platinum: Write an app to count down from 10 to 0 (without the Micro:bit needing to be touched), then show a smiley face.
    • BONUS CHALLENGE: If the user pushes one of the buttons during the countdown, it should show a sad face instead. Once a button is pushed, the numbers should stop counting.