hits, turns)for loops and if inside listsReal games donβt end after one guess! They track score, lives, and progress. Learning to manage game state is how you build Pac-Man, Minecraft, or Roblox games!
Lists and loops are used in every programming language β from apps to robots.
import random
ships = []
while len(ships) < 3:
r = random.randint(0, 4)
c = random.randint(0, 4)
if [r, c] not in ships:
ships.append([r, c])
print("3 ships hidden! 10 turns to find them all.")
hits = 0
for turn in range(10):
print("\nTurn", turn + 1)
guess_row = int(input("Row (0-4): "))
guess_col = int(input("Col (0-4): "))
if [guess_row, guess_col] in ships:
print("π― HIT!")
ships.remove([guess_row, guess_col])
hits += 1
if hits == 3:
print("π You win!")
break
else:
print("π¦ MISS!")
if hits < 3:
print("Game over! You found", hits, "ships.")
row_letter = input("Row (A-E): ").upper()guess_row = ord(row_letter) - ord('A')Youβve learned the core ideas behind almost every game: hidden objects, player input, feedback, and win/lose conditions.