Coding Basics

Variables and Logic

 

Think of variables as little boxes where your robot can store information.


🧠 What is a variable?

  • A variable is a name that holds a value (like a number or a word).
  • It’s like writing a label on a box and putting something inside.
  • You can open the box later and see what’s inside, or change it.

🟡 Example Mission: Store your name

Blocks used:

  • Set variable: Create a box with a name.
  • Serial print: Show what’s inside.

MicroPython Code:

# Create a variable called 'name' and store a word
name = "Alex"                   # Put "Alex" inside the box called name

# Show the value of the variable
print("My name is:", name)      # Print the contents of the box

🎯 Your robot just remembered something!


🔁 Example Mission: Change a value

# Store a number in a variable
score = 10                      # Start with 10 points

# Change the value
score = score + 5               # Add 5 more points

print("Your score is:", score)  # Show the new score

🎯 Variables can change, just like your game score!



🎓 Mini-Tutorial: Logic

Logic blocks help your robot think and compare things.


🧠 What is logic?

  • Logic is about true or false.
  • It’s how your robot decides if something is correct, bigger, equal, or different.
  • Think of it as asking yes/no questions.

🟡 Example Mission: Compare numbers

Blocks used:

  • Equal (==): Checks if two things are the same.
  • Greater than (>): Checks if one number is bigger.
  • Less than (<): Checks if one number is smaller.

MicroPython Code:

# Compare two numbers
x = 5
y = 3

print(x == y)   # False, because 5 is not equal to 3
print(x > y)    # True, because 5 is greater than 3
print(x < y)    # False, because 5 is not less than 3

🎯 Your robot can answer yes/no questions with True or False!


🔄 Example Mission: Use logic with if

temperature = 25                 # Store a value in a variable

if temperature > 20:             # Is it greater than 20?
    print("It’s warm!")          # Yes → print this
else:
    print("It’s cold!")          # No → print this

🎯 Logic helps your robot make smart choices!


🚀 Why these matter

  • Variables let your robot remember things.
  • Logic lets your robot think and decide.
    Together with Control blocks, they form the brain of your robot.
On this page