Back

Activity 1.13: While Loops

divider

Activity 1.13

While Loops

Key Concepts

Iteration

The while Loop

The while Loop

A while loop repeats its block as long as its condition stays True.

timer = 10
while timer > 0:
print(timer)
timer = timer - 1

A Common Bug: The Infinite Loop

If nothing inside the loop ever changes the condition, it never stops.

number = 1
while number <= 100:
print(number)
# Forgot to update number -- this runs forever!

Always Update the Condition

number = 1
while number <= 100:
print(number)
number = number + 1

Every while loop needs a way to eventually become False.

Today's Objectives

  • Writing a while loop
  • Avoiding infinite loops by updating the condition
  • Using a loop to drive a menu-based program

Key Terms

Iteration
Repeating a block of code multiple times.
while Loop
Repeats its block as long as its condition is True.
Infinite Loop
A loop whose condition never becomes False, so it never stops.

'F' → Fullscreen

divider

Build

Create a new Python program named 1-13-while-loops.


Task 1: 99 Bottles Song

  • Print the classic song, counting down one bottle per loop.
While Loops
import random
print("--- Demo 1 - 99 Bottles Song ---")
soda = input("Enter your favorite soda:")
bottles = 99
while bottles > 0:
print(f"{bottles} bottles of {soda} on the wall,")
print(f"{bottles} bottles of {soda}!")
print("You take one down, pass it around,")
bottles = bottles - 1
print(f"{bottles} bottles of {soda} on the wall!")
input("Press enter to continue...")

Task 2: Turn-Based Battle

  • Build a menu-driven battle against a goblin, looping until one side is defeated.
While Loops
import random
print("--- Demo 1 - 99 Bottles Song ---")
soda = input("Enter your favorite soda:")
bottles = 99
while bottles > 0:
print(f"{bottles} bottles of {soda} on the wall,")
print(f"{bottles} bottles of {soda}!")
print("You take one down, pass it around,")
bottles = bottles - 1
print(f"{bottles} bottles of {soda} on the wall!")
input("Press enter to continue...")
print("--- Demo 2 - Turn-Based Battle ---")
player_health = 10
enemy_health = 6
fighting = True
while fighting:
print("Health:", player_health)
print("Goblin:", enemy_health)
print("-Menu-")
print("1) Attack")
print("2) Heal")
choice = input("->").lower()
if choice == "1" or choice == "attack":
print("Player attacks!")
player_attack = random.randint(0, 5)
if player_attack > 0:
print(f"You did {player_attack} damage.")
enemy_health = enemy_health - player_attack
else:
print("Missed!")
elif choice == "2" or choice == "heal":
potion = random.randint(1, 3)
print(f"Healed {potion} points.")
player_health = player_health + potion
else:
print("Invalid option. You lose your turn!")
if enemy_health > 0:
print("Goblin attacks!")
enemy_attack = random.randint(0, 3)
if enemy_attack > 0:
print(f"Goblin did {enemy_attack} damage.")
player_health = player_health - enemy_attack
else:
print("Missed!")
else:
print("Goblin defeated!")
fighting = False
if player_health <= 0:
print("You were defeated!")
fighting = False
divider

Checkpoint

Verify your program works correctly.

Example Output
--- Demo 1 - 99 Bottles Song ---
Enter your favorite soda: Code Zero [Enter]
99 bottles of Code Zero on the wall,
99 bottles of Code Zero!
You take one down, pass it around,
98 bottles of Code Zero on the wall!
...
Press enter to continue...
--- Demo 2 - Turn-Based Battle ---
Health: 10
Goblin: 6
-Menu-
1) Attack
2) Heal
-> 1
Player attacks!
You did 3 damage.
Goblin attacks!
Missed!
Health: 10
Goblin: 3
-Menu-
1) Attack
2) Heal
-> 1
Player attacks!
You did 4 damage.
Goblin defeated!
divider

Reflection

Answer the following questions before submitting your work.

  1. What line in the 99 Bottles loop is responsible for eventually ending it? What would happen if you removed it?
  2. In the Turn-Based Battle, what condition keeps the while fighting: loop running?
  3. Why does the battle loop check enemy_health and player_health at the end of every pass, instead of just once at the start?
divider

Submit

Submit the required files to the appropriate dropbox.

Activity Complete