Tutorial 3: Smart Bearcu
Learning Goal: Conditionals (If/Else)
Sometimes you need to make decisions in your code. Should Bearcu move forward or turn? It depends on whether there's a wall!
The if statement lets you run code only when a condition is
true. The else statement runs when the condition is false.
if bearcu.can_move():
bearcu.move_forward() # Only if path is clear
else:
bearcu.turn_right() # If there's a wallYou can also use not to check the opposite:
if not bearcu.can_move():
bearcu.say("Wall ahead!") # If there IS a wallTutorial 3: Smart Bearcu
Bearcu encounters a wall blocking the direct path to the star!
You need to check if Bearcu can move before moving. If there's a wall, you'll need to go around it.
Use bearcu.can_move() to check if the path ahead is clear, and bearcu.turn_right() or bearcu.turn_left() to change direction.
Initializing Python environment...
Loading grid...
What You Learned
- Use
ifto run code conditionally - Use
elseto handle the alternative case bearcu.can_move()returns True or Falsenotreverses a boolean value- Combine conditionals with loops for smart behavior