forked from realpython/materials
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadventure_game.py
More file actions
26 lines (22 loc) · 802 Bytes
/
Copy pathadventure_game.py
File metadata and controls
26 lines (22 loc) · 802 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import random
health = 5
enemy_health = 3
while health > 0 and enemy_health > 0:
# Normalize input to handle extra spaces and case variations.
action = input("Attack or Run? ").strip().lower()
if action not in {"attack", "run"}:
print("Invalid choice. Please type 'Attack' or 'Run'.")
continue
if action == "attack":
enemy_health -= 1
print("You hit the enemy!")
# Implement a 50% chance that the enemy strikes back.
enemy_attacks = random.choice([True, False])
if enemy_attacks:
health -= 2
print("The enemy strikes back!")
else:
print("You ran away!")
break
print(f"Your health: {health}, Enemy health: {enemy_health}")
print("Victory!" if enemy_health <= 0 else "Game Over")