Conditionals
python ·Understanding Indentation
Remember how important indentation is in Python?
for i in range(5):
basic.show_string("Hello!") # This line is indented - it's INSIDE the loop
The space at the beginning of the second line is called indentation. It tells Python that this code belongs inside the loop.
What happens if we don’t indent?
for i in range(5):
basic.show_string("Hello!") # This line is NOT indented - it's OUTSIDE the loop
This only prints “Hello!” once instead of 5 times! Why?
Answer: Because basic.show_string("Hello!") is outside of the loop.
Your Turn!
What does this print out?
for i in range(5):
basic.show_string("Hello!")
basic.show_string("Bye!")
Answer
5 lines of “Hello!” followed by 1 line of “Bye!”
The “Hello!” is inside the loop (indented), so it repeats 5 times. The “Bye!” is outside the loop (not indented), so it only prints once.
Conditional Operators!
We’ve seen arithmetic operators before:
+adding-subtracting*multiplying/dividing
Now let’s learn about comparison operators, which compare two values:
>greater than<less than==is equal to!=is not equal to>=greater than or equal to<=less than or equal to
Pop Quiz!
Is this true or false?
1000 > 3
Answer
True!
1000 is greater than 3.
Is this true or false?
9 < 8
Answer
False!
9 is not less than 8.
Is this true or false?
5 == 5
Answer
True!
5 equals 5.
Is this true or false?
45 == 93
Answer
False!
45 does not equal 93.
If Statements
What if we wanted to show something based on a condition? For example:
- Happy face if number is 1
- Sad face if number is 2
We can use if statements! They look like this:
num = 1
if (num == 1):
basic.show_icon(IconNames.HAPPY)
Elif (Else If)
elif means “else, if”. In this case, if num isn’t 1, we check if num is 2:
num = 1
if (num == 1):
basic.show_icon(IconNames.HAPPY)
elif(num == 2):
basic.show_icon(IconNames.SAD)
Else
If num is neither 1 or 2, then we use else:
num = 1
if (num == 1):
basic.show_icon(IconNames.HAPPY)
elif(num == 2):
basic.show_icon(IconNames.SAD)
else:
basic.show_icon(IconNames.MEH)
Your Turn!
Try copying and then changing num to 1, 2, and 3 before running your code:
num = 1
if (num == 1):
basic.show_icon(IconNames.HAPPY)
elif(num == 2):
basic.show_icon(IconNames.SAD)
else:
basic.show_icon(IconNames.MEH)
Random Numbers
We can also make random numbers!
number = randint(1, 10)
This will generate a random number from 1 to 10.
Pop Quiz!
What numbers will this generate?
number = randint(1, 3)
Answer
Numbers from 1 to 3!
randint(1, 3) generates: 1, 2, or 3.
Your Turn: Make a Magic 8 Ball! 🔮
Let’s create a Magic 8 Ball that gives random answers when you shake it!
The Plan
Forever:
- If the user shakes the micro:bit
- Generate a new random image from these options:
- Yes
- No
- Meh
Step 1: Start a Forever Loop
while True:
if input.is_gesture(Gesture.SHAKE):
# Generate a new random image from these options: Yes, No, Meh
Step 2: Add Random Number Generation
while True:
if input.is_gesture(Gesture.SHAKE):
number = randint(1, 3) # will give us a random number: 1, 2, or 3
if number == 3:
# YES
elif number == 2:
# NO
else:
# MEH
Step 3: Add the Icons
while True:
if input.is_gesture(Gesture.SHAKE):
number = randint(1, 3)
if number == 3:
basic.show_icon(IconNames.YES)
elif number == 2:
basic.show_icon(IconNames.NO)
else:
basic.show_icon(IconNames.MEH)
Complete Magic 8 Ball Code
while True:
if input.is_gesture(Gesture.SHAKE):
number = randint(1, 3)
if number == 3:
basic.show_icon(IconNames.YES)
elif number == 2:
basic.show_icon(IconNames.NO)
else:
basic.show_icon(IconNames.MEH)
Try it out! Shake your micro:bit and see what answer you get! 🔮
Customize Your Magic 8 Ball
Try these modifications:
- Add more answers:
randint(1, 5)and moreelifstatements - Add different icons:
IconNames.HAPPY,IconNames.SAD,IconNames.ANGRY - Add a pause:
basic.pause(1000)after showing the icon - Clear the screen:
basic.clear_screen()before the next shake
Extra Challenges
Try these challenges:
- Make a dice roller that shows numbers 1-6
- Create a mood detector that responds to different gestures
- Build a simple calculator using buttons
- Make a game that keeps score using variables