โ† Back to Basics

Simple Battle

8 of 8

๐ŸŽฎ BONUS: Simple Battle

๐Ÿ”„ While Loops ๐Ÿ”€ Conditionals

Your Task

Make a battle between Player (100 HP) and Goblin (60 HP).

Use a while loop:

  • Print both healths
  • Ask "Attack? (yes/no)"
  • If yes: Goblin loses 20 HP, then Player loses 10 HP
  • If no: break
  • Loop until someone reaches 0 HP

Print who wins!


Example Output

Player: 100 HP | Goblin: 60 HP
Attack? (yes/no): yes

Player: 90 HP | Goblin: 40 HP
Attack? (yes/no): yes

Player: 80 HP | Goblin: 20 HP
Attack? (yes/no): yes

Player: 70 HP | Goblin: 0 HP
You win!

Input Lines (for browser)

yes yes yes

Try it yourself

Code: Simple Battle

Loading Python runtimeโ€ฆ
Python โ€ฆ
Loading...
bash
$ Click "Run" to execute your code...