← Back to Loops Part 2
πŸ‰

Boss Battle

5 of 6

Exercise 8: Boss Battle

Time for a boss fight! You're battling a boss with 50 HP. Your attack does 12 damage per turn. But the boss raises a shield every 3rd turn (turns 3, 6, 9, ...), blocking your attack.

Use a while True loop with continue for shield turns and break when the boss is defeated.

boss_hp = 50 max_hp = 50 player_attack = 12

Expected output:

Turn 1: You attack! Boss HP: 38/50
Turn 2: You attack! Boss HP: 26/50
Turn 3: Boss shields! Attack blocked.
Turn 4: You attack! Boss HP: 14/50
Turn 5: You attack! Boss HP: 2/50
Turn 6: Boss shields! Attack blocked.
Turn 7: You attack! Boss defeated!
Boss defeated in 7 turns!

Requirements

  • Use a while True loop (infinite loop with break to exit)
  • Track the turn number (starting from 1)
  • Every 3rd turn (turn % 3 == 0), the boss shields β€” print the shield message and continue
  • On attack turns, subtract player_attack from boss_hp
  • If boss HP drops to 0 or below, print the defeat message and break
  • After the loop, print how many turns it took
  • No starter code β€” write the whole solution yourself!

Hints

Hint 1 β€” Setting up the while True loop
turn = 0 while True: turn += 1 # ... your logic here

Increment the turn at the start of each iteration so Turn 1 is the first thing that happens.

Hint 2 β€” Shield check comes first

Check for the shield before attacking. If it's a shield turn, print the message and continue to skip the attack:

if turn % 3 == 0: print(f"Turn {turn}: Boss shields! Attack blocked.") continue
Hint 3 β€” Defeat check

After reducing boss_hp, check if it's 0 or below. If so, print the defeat message (not the HP readout) and break:

boss_hp -= player_attack if boss_hp <= 0: print(f"Turn {turn}: You attack! Boss defeated!") break

Otherwise, print the normal attack message with the remaining HP.

πŸ’»

Try it yourself

Code: Boss Battle

Loading Python runtime…
Python …
Loading...
Output
Click "Run" to execute your code...
ℹ️ About this Python environment

βœ… Standard library: heapq, collections, itertools, math, random, functools, datetime, bisect

βœ… Functions, classes, recursion, print()

❌ No file system, subprocess, OS access, or network requests

❌ No pip install (all supported modules are pre-loaded)

⏱️ 5 second execution time limit