Hello everyone. This is my Battleships program for python. I currently want to make the program to display how many ships are left after each turn. The way I've coded is that it tells me after the second turn. However I want to make it to display the message after each turn. If anyone can help that would be much appriciated
import random
store =
for x in range(9):
store.append(random.randint (0 , 8))
for y in range(9):
store.append(random.randint (0 , 8))
#Setup Global Variables------------------
torpedos = 30
theSea = [["_" for i in range(9)] for j in range(9)] #2D list 9x9
theSea[x][y] = "S" #Place the ship in position x,y
ships = 10 #How many ships to find
#Define procedures and functions --------------------------------
def displayGrid():
print()
print(" 1 2 3 4 5 6 7 8 9 ") #display column headings
for y in range(0,9):
print((y+1), " " , end="") #print without new line
for x in range(0,9):
if (theSea[x][y] =="S"):
print("_", " " , end="") #hide the ship
else:
print(theSea[x][y] , " " , end="") #print contents of array
print() #next line
print()
#begin main thread -------------------------------------------------
name = input("What is your name:")
print("Welcome to BATTLESHIPS!" + name)
print("10 enemy ships are hidden somewhere at sea")
print("You only have 30 torpedos to find it!")
while ships > 0 and torpedos > 0:
displayGrid()
print()
print("You have " +str(torpedos) + " torpedos left.")
print("Target coordinates, Captain?")
refx = int(input("Enter the x coordinate: "))
refy = int(input("Enter the y coordinate: "))
refx = refx -1 #computers count from 0
refy = refy -1 #this could be done with fewer lines
if (theSea[refx][refy] == "S"):
theSea[refx][refy] = "X"
print(" DIRECT HIT, CAPTAIN!")
ships = ships - 1
else:
theSea[refx][refy] = "M"
print("You missed..." + name)
print("You will waste tropedos if you fire here again!")
torpedos = torpedos - 1
input() #wait for user to press enter before redrawing grid
if ships == 0:
print("Huzah! You sunk all the ships.")
else:
print("You ran out of torpedos" + name)
print("GAME OVER" + name )
print("It took {} torpedos to sink this ship".format(torpedos))
import random
store =
for x in range(9):
store.append(random.randint (0 , 8))
for y in range(9):
store.append(random.randint (0 , 8))
#Setup Global Variables------------------
torpedos = 30
theSea = [["_" for i in range(9)] for j in range(9)] #2D list 9x9
theSea[x][y] = "S" #Place the ship in position x,y
ships = 10 #How many ships to find
#Define procedures and functions --------------------------------
def displayGrid():
print()
print(" 1 2 3 4 5 6 7 8 9 ") #display column headings
for y in range(0,9):
print((y+1), " " , end="") #print without new line
for x in range(0,9):
if (theSea[x][y] =="S"):
print("_", " " , end="") #hide the ship
else:
print(theSea[x][y] , " " , end="") #print contents of array
print() #next line
print()
#begin main thread -------------------------------------------------
name = input("What is your name:")
print("Welcome to BATTLESHIPS!" + name)
print("10 enemy ships are hidden somewhere at sea")
print("You only have 30 torpedos to find it!")
while ships > 0 and torpedos > 0:
displayGrid()
print()
print("You have " +str(torpedos) + " torpedos left.")
print("Target coordinates, Captain?")
refx = int(input("Enter the x coordinate: "))
refy = int(input("Enter the y coordinate: "))
refx = refx -1 #computers count from 0
refy = refy -1 #this could be done with fewer lines
if (theSea[refx][refy] == "S"):
theSea[refx][refy] = "X"
print(" DIRECT HIT, CAPTAIN!")
ships = ships - 1
else:
theSea[refx][refy] = "M"
print("You missed..." + name)
print("You will waste tropedos if you fire here again!")
torpedos = torpedos - 1
input() #wait for user to press enter before redrawing grid
if ships == 0:
print("Huzah! You sunk all the ships.")
else:
print("You ran out of torpedos" + name)
print("GAME OVER" + name )
print("It took {} torpedos to sink this ship".format(torpedos))