Kids Coding
Draw with Code
Print stars, triangles, and rows. Loops make pictures out of text.
Pictures from text
A loop can print a row of stars. Multiply a string: "*" * 5 is *****.
A row
print("*" * 5)Change one number in the loop and the whole drawing grows or shrinks. Big art from one small tweak.
A rectangle
Four rows
for n in range(4):
print("*" * 6)A triangle
Let the row get longer each time. Use the loop variable.
Growing rows
for n in range(1, 6):
print("*" * n)Real life
Drawing a fence on a card with the same stick, over and over.
One stick, repeated. A loop prints the fence faster than you can draw it.
Garden fence
print("+" + "-" * 10 + "+")
for n in range(3):
print("|" + " " * 10 + "|")
print("+" + "-" * 10 + "+")Change one word. Run it. That is how this idea shows up outside the lesson.
Try it
Exercise 1: Print a triangle of # that is 4 rows tall.
Show solution
for n in range(1, 5):
print("#" * n)