rAs Kids All courses
Robotics with Python ยท Module 4 of 8
๐Ÿ“ฑ Shared phone

Python in the Browser

Variables, loops and functions are the only three things you need before you can control anything.

๐Ÿ“ฑ for i in range(4): forward()
A phone in landscape showing four lines of Python and the printed output beneath.

๐ŸŽ’ What you need

โœ‹ Do this

  1. Write code on paper first. Typing on a phone is slow, so make the slow part count โ€” this habit alone will save you hours.
  2. Type: speed = 50, then print(speed). Change it to 80 and print again. A variable is a labelled box you can change.
  3. Write a loop that prints the numbers 1 to 4. Then wrap your square-driving commands in it, printing 'forward' and 'turn' each time.
  4. Write a function drive_square(steps) that takes a number and prints the right instructions. Call it twice with different numbers.
  5. Add an if statement: if battery < 20, print 'RETURN HOME'. Set battery to different values and watch which branch runs.
  6. Now combine all four into a fake robot main loop that runs ten times, printing sense, decide and act each cycle. That printout is module 1 in code.

๐Ÿ’ก Why it works

Robot code is not exotic. It is variables holding sensor readings, loops running the control cycle, if statements choosing behaviour, and functions keeping it readable. Writing it on paper first is what professionals do when hardware time is expensive โ€” and a shared phone is expensive time.

๐Ÿ”ฅ Challenge

Write a function that takes a list of sensor readings and returns the average. Then use it to smooth ten noisy numbers. Noisy sensors are the normal case, not the exception.

๐Ÿ“– New words

variablea named place that holds a value you can change
functiona reusable named block of code that can take inputs
conditionalcode that only runs when a test is true
๐Ÿ
Python Starter

Tap when you have finished this module.