Kids Coding
Think in Steps
An algorithm is a plan. Search a list. Sort a few numbers. Count how many steps.
A plan, then code
An algorithm is the steps. Code is one way to write them. Search a list: look at each item until you find the target — or you run out.
Find a name
names = ["Sam", "Riley", "Jun"]
target = "Jun"
found = False
for name in names:
if name == target:
found = True
break
if found:
print("Found", target)
else:
print("Not here")Write the steps in plain words first, then turn each line into Python. Solving the puzzle and writing the code are two different jobs, so do them one at a time.
Count the steps
This list is tiny, so the loop is instant. The idea still matters: each check is a step. Bigger lists take more steps.
How many looks
numbers = [4, 1, 9, 2]
steps = 0
for n in numbers:
steps = steps + 1
if n == 9:
break
print("Found after", steps, "looks")Real life
Finding a book on a messy shelf, left to right.
You look at each spine until you find it — or the shelf ends. That loop is a search.
Find the book
shelf = ["Maths", "Foxes", "Maps"]
want = "Foxes"
for book in shelf:
print("Looking at", book)
if book == want:
print("Got it")
breakChange one word. Run it. That is how this idea shows up outside the lesson.
Try it
Exercise 1: Loop [3, 8, 1] and print yes if you see 8.
Show solution
for n in [3, 8, 1]:
if n == 8:
print("yes")