The Adventures of Ava the Astronaut: Learning Python One Planet at a Time.
Part 4 Conceptual Complete

Part 4: The Looping Nebula — Repeating Actions

The Adventures of Ava the Astronaut: Learning Python One Planet at a Time

Updated May 29, 2025 6 min read

Part 4: The Looping Nebula — Repeating Actions

The Adventures of Ava the Astronaut: Learning Python One Planet at a Time


A Tedious Task

After learning to make decisions, Ava piloted her ship towards a breathtaking sight: the Looping Nebula. It was a cosmic cloud of swirling gas and dust, filled with shimmering space crystals that her ship could collect for fuel. The mission was simple: collect 100 crystals.

Ava started writing the code:

print("Collecting crystal 1...")
print("Collecting crystal 2...")
print("Collecting crystal 3...")
# ... and so on, 97 more times?!

She stopped, sighing. “Py, this is going to take forever! I have to write the same line of code 100 times!”

Py whirred. “Affirmative, Commander. You have discovered the need for loops. A loop is a way to repeat a block of code multiple times without having to write it over and over. It is one of the most powerful tools in all of programming.”


The for Loop: Repeating a Set Number of Times

The most common type of loop is the for loop. It is perfect for when you know exactly how many times you want to repeat an action. To help Ava collect her crystals, we can use a for loop together with the range() function.

The range() function generates a sequence of numbers. range(5) produces the numbers 0, 1, 2, 3, 4 — that is, five numbers starting from zero. Let’s start small and collect just 5 crystals.

for i in range(5):
print(f"Collecting crystal {i + 1}...")
print("Collection complete!")

What happens?

The output will be:

Collecting crystal 1...
Collecting crystal 2...
Collecting crystal 3...
Collecting crystal 4...
Collecting crystal 5...
Collection complete!

This small block of code does the same job as five separate print statements! And if Ava needs 100 crystals, she just changes range(5) to range(100). Let’s break down how it works:

ComponentWhat It Does
forStarts the loop.
iA loop variable. Each time the loop repeats, i takes the next value from the sequence.
in range(5)Defines the sequence: the numbers 0, 1, 2, 3, 4.
:Marks the beginning of the loop body.
The indented codeThe loop body — the code that gets repeated each time.

We use i + 1 in the print statement because range(5) starts counting at 0, but we want our crystal count to start at 1.


Looping Through Collections

The for loop is not just for numbers. It can loop through any collection, like a list of items or even the characters in a string!

# Looping through a list of planets
planets = ["Mercury", "Venus", "Earth", "Mars"]
for planet in planets:
print(f"Now scanning: {planet}")

Output:

Now scanning: Mercury
Now scanning: Venus
Now scanning: Earth
Now scanning: Mars

Each time the loop runs, the variable planet takes the next value from the planets list. This is incredibly useful for processing collections of data.


The while Loop: Repeating Until a Condition is Met

Sometimes, you don’t know in advance how many times you need to loop. You just need to keep going until a certain condition becomes false. This is what the while loop is for.

Imagine Ava is approaching a space station, but she needs to slow down. She must keep firing her retro-rockets while her speed is too high.

ship_speed = 1000
while ship_speed > 200:
print(f"Current speed: {ship_speed}. Firing retro-rockets!")
ship_speed = ship_speed - 100 # Decrease speed by 100
print(f"Speed is now {ship_speed}. Cleared for docking.")

What happens?

The loop first checks: is ship_speed > 200? Since 1000 is greater than 200, the answer is True, so it runs the loop body. It prints the speed and then reduces it by 100, making it 900. Then it goes back to the top and checks the condition again (is 900 > 200? Yes!). This continues until ship_speed becomes 200. At that point, the condition 200 > 200 is False, so the loop stops and the final message is printed.

Warning: The Infinite Loop! Beware of the dreaded infinite loop! If the condition in a while loop never becomes False, the loop will run forever, and your program will get stuck. For example, if we forgot the line ship_speed = ship_speed - 100, the speed would never decrease, the condition would always be True, and the loop would never end. If this happens to you, press Ctrl + C on your keyboard to force the program to stop.


Controlling the Loop: break and continue

Sometimes you need more control over a loop. Python gives you two special keywords for this.

break immediately exits the loop, even if the condition is still true. Imagine Ava is scanning asteroids for a rare mineral. She wants to stop scanning as soon as she finds it.

asteroids = ["rock", "ice", "rock", "rare mineral", "rock"]
for asteroid in asteroids:
print(f"Scanning: {asteroid}")
if asteroid == "rare mineral":
print("Found it! Stopping scan.")
break # Exit the loop immediately

continue skips the rest of the current iteration and jumps to the next one. Imagine Ava wants to scan all asteroids but skip the boring “rock” ones.

asteroids = ["rock", "ice", "rock", "rare mineral", "rock"]
for asteroid in asteroids:
if asteroid == "rock":
continue # Skip this one, go to the next
print(f"Interesting find: {asteroid}")

Output:

Interesting find: ice
Interesting find: rare mineral

Mini-Project: “Asteroid Dodge” Countdown

An asteroid is headed straight for Ava’s ship! Your mission is to write a program that creates a dramatic 10-second countdown to evasive maneuvers.

Your Task:

  1. Use a for loop and range() to count down from 10 to 1. (Hint: range() can take a start, stop, and step value. Try range(10, 0, -1) to count backwards!)
  2. Inside the loop, print the current countdown number.
  3. After the loop finishes, print a dramatic message like “EVASIVE MANEUVERS! The asteroid has been dodged!”

Expected Output:

10...
9...
8...
7...
6...
5...
4...
3...
2...
1...
EVASIVE MANEUVERS! The asteroid has been dodged!

Bonus Challenge: Can you write the same countdown using a while loop instead?


Mission Complete!

You have successfully navigated the Looping Nebula! You have learned how to automate repetitive tasks using both for loops (for a known number of repetitions) and while loops (for repeating until a condition changes). You also learned to control loops with break and continue. Your programming skills are becoming more powerful with every mission.

Next, we are heading to “Mission Control,” where we will learn how to create our own reusable commands with Functions, making our code more organized and efficient! See you in Part 5!


This is Part 4 of a 10-part series: “The Adventures of Ava the Astronaut: Learning Python One Planet at a Time.”