Make it say hello 5 times
A loop repeats a greeting five times using two lines. The indentation matters because it tells Python which instruction belongs inside the loop.
- Code
- Starter
- About 10 minutes
Grown-up needed: yes for choosing and supervising the coding environment
The film says 'Taz, on the Code tab'. In Taz today, choose Make, then Code, and pick Python.
What you need
- Paper and a pencil
- A grown-up-approved Python environment where the steps use code
- A grown-up
- Or the Maker Studio in Taz, in Python mode: open Taz, choose Make, then Code
The rule
Only two lines allowed.
How to do it
- Write for i in range(5): on the first line.
- Indent the next line and write print("Hello").
- Predict the number of output lines.
- Run the two lines in a grown-up-approved Python environment.
- Count the greetings and compare with your prediction.
Why it works
Python’s range(5) supplies five values, starting at zero and ending at four. The loop takes each value in turn and runs its indented instruction. Printing once during each turn therefore produces five greetings. The variable i need not appear in the greeting for the repetition to work. Indentation identifies the loop body, so moving the print instruction outside it changes when that instruction runs. This example explores repetition without needing five separate print statements.
Make it harder
- Change five repetitions to eight.
- Print the loop value beside the greeting.
- Predict what happens when the print line is outside the loop.
Ask Taz
- Why does range(5) start with zero?
- Why must the print line be indented?
- Why does one instruction produce several output lines?
The coding part of Taz is the Maker Studio.
For grown-ups
Choose and supervise the coding environment. The film names a Taz Code tab. In Taz today that is the Maker Studio: choose Make, then Code, and pick Python. Use ordinary sample words and numbers without personal details. Ask at dinner: “Which prediction did you check, and what change in the instructions would change that output?”
Taz is an AI and can be wrong, so stay close by. Read how safety works on Taz.
Show us yours
Film your hands and what you made, not your face or name. Ask a grown-up before posting anywhere else.
Questions parents ask
Can we begin without running code?
Yes. Write the predicted output on paper and trace the steps together. That gives you a comparison to use when a grown-up-approved Python environment is available. A prediction remains a prediction until you check the actual instructions and result.
What if an error appears?
Read the message with a grown-up and compare spelling, punctuation and indentation with the steps. Change one thing at a time and keep a note of what happened. An error is useful evidence about the instructions, not a judgement about the child.
Must we share the result?
No. Discussing the output at home is enough. Follow the privacy line above for optional online sharing.
More to try
See all Taz Challenges, or look through the science labs. One place for every why. Ask for early access at aitaz.com