← Back to course
Code Crew G6+ / Week 04 / Crew Quest
6/6
Week 04 Β· Loops

Crew Quest

Crew Quest: Loop art
// for, while, break and a countdown clock
⏱ about 30 min

Saturday: Crew Quest: Loop art

The crew wants a picture of a rocket on the welcome screen, but nobody has a drawing program at Mission Control. Rocket looks at his loops and has an idea.

"What if the loop draws the picture?" he says. "One row at a time, with stars and spaces."

Raven sketches a small triangle on graph paper and counts the squares. "What do you notice?" she asks. "Each row has two more stars than the row above, and one less space in front."

Nova draws the triangle in glowing dots, row by row. "I found a pattern," she says. "When you can describe a row with the row number, a loop can draw it."

Rocket writes the row rule in his notebook before writing any code.

  1. for runs a block once for each number in a range. range stops before the stop number.
  2. A variable made before a loop can collect a total or count down.
  3. while repeats as long as its condition is True. Make sure something can make it False.
  4. while True with break asks again and again until the answer is right.
  5. import time and time.sleep(1) make a program wait one second.
# nose_cone.py
for row in range(1, 5):
    spaces = " " * (4 - row)
    stars = "*" * (2 * row - 1)
    print(spaces + stars)
   *
  ***
 *****
*******
How many stars are in row 6 if you change range(1, 5) to range(1, 7) and the 4 to 6?
WHY THIS EXERCISERow 6 has 2 times 6 minus 1 stars, which is 11.
β—‡ LOOP ART STUDIO
With a grown-up, design a picture made of rows: a tower, a staircase or a flag.
Write the rule for each row in words first: how many spaces, how many symbols.
Turn the rule into a for loop in IDLE. Run it and adjust until it looks right.
Swap programs and predict what the other person's loop will draw before running it.
MORE REVIEW: WEEK 4
  • Read the question.
  • Tap your answer.
How many times does for i in range(2, 8) run?
Which loop fits: keep asking until the user types go?
total = 0, then the loop adds 1, 2 and 3. What is total?

Wonderful work. Next week you will keep many values together in lists and dictionaries.

← Friday