๐ฑ for i in range(4): forward()
๐ What you need
- the shared phone with a browser Python editor, or an offline Python app such as Pydroid 3
- phone in landscape and, if possible, a keyboard app with a number row
- paper to write code out before typing it
โ Do this
- Write code on paper first. Typing on a phone is slow, so make the slow part count โ this habit alone will save you hours.
- Type: speed = 50, then print(speed). Change it to 80 and print again. A variable is a labelled box you can change.
- Write a loop that prints the numbers 1 to 4. Then wrap your square-driving commands in it, printing 'forward' and 'turn' each time.
- Write a function drive_square(steps) that takes a number and prints the right instructions. Call it twice with different numbers.
- Add an if statement: if battery < 20, print 'RETURN HOME'. Set battery to different values and watch which branch runs.
- 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.