If / Else: Choose Your Path!
Bit reaches a locked door. How does a computer choose?
Real Python code
This is the code from the video. Read it out loud like a recipe, one line at a time!
keys = 1
if keys == 1:
print("The door opens!")
else:
print("I need a key!")
print("Keep exploring!")
Quiz time!
Bit has 0 keys. What will Bit say?
keys = 0
if keys == 1:
print("Door opens!")
else:
print("Need a key!")
Where if-else hides
Games
If you touch a coin, your score goes up
Night lights
If it's dark, turn on. Else, stay off
Sliding doors
If someone is near, slide open
Passwords
If the password matches, you're in
One = or two?
Oops!
- if keys = 1:
- One = puts a value in
- Python says: error!
Fixed!
- if keys == 1:
- Two == asks a question
- Python picks a path
One = puts. Two == asks.
Try it yourself!
Open Scratch and use an if then else block so your sprite says one thing when a key is pressed, and something else when it isn't.
Bit is working on the next one: Variables: boxes that remember things