Untitled
unknown
plain_text
a year ago
1.9 kB
9
Indexable
import random computerShips = [] playerShips = [] computerWater = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] playerWater = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] while len(computerShips) < 3: pick = random.randint(0, 9) if pick not in computerShips: computerShips.append(pick) while len(playerShips) <3: pick = int(input("Pick a number from 0 to 9: ")) if pick not in playerShips: playerShips.append(pick) # Resume with the rest of the starter code while len(computerShips) > 0 and len(playerShips) > 0: print("Remaining player ships:", len(playerShips)) print("Remaining computer ships:", len(computerShips)) print("Computer water: ", computerWater) print("Player water: ", playerWater) while True: playerFire = int(input("Pick a target to shoot: ")) if playerFire in computerWater: computerWater[playerFire] = "X" break else: print("Invalid target. Try again") while True: computerFire = random.randint(0, 9) if computerFire in playerWater: playerWater[computerFire] = "X" break # Resume with the rest of the starter code print("You shoot at", playerFire) print("The computer shoots at", computerFire) if playerFire in computerShips: print("The computer has been hit!") computerShips.remove(playerFire) else: print("You have missed") # Resume with the rest of the starter code if computerFire in playerShips: print("You've been hit!") playerShips.remove(computerFire) else: print("The computer missed!") print("\n\n\n") if len(computerShips) == len(playerShips): print("It's a tie!") elif len(playerShips) > len(computerShips): print("You win!") else: print("You lose!")
Editor is loading...
Leave a Comment