# Danger, Will Robinson! Why wont it print out X?

Hi, i have just been doing the build a battleships game on the python 2 course but from what i understand after i have followed the instructions it should print out a board with an X on it once the user has input guess row and guess column.

#Here is my code
from random import randint

board =

for x in range(0, 5):
board.append([“O”] * 5)

def print_board(board):
for row in board:
print " ".join(row)

print_board(board)

def random_row(board):
return randint(0, len(board) - 1)

def random_col(board):
return randint(0, len(board[0]) - 1)

ship_row = random_row(board)
ship_col = random_col(board)
print ship_row
print ship_col
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

if guess_row == ship_row and guess_col == ship_col:
print “Congratulations! You sank my battleship!”
else:
print “You missed my battleship!”
board[guess_row][guess_col] = “X”
print_board(board)

#Here is the output?
O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
3
0
Guess Row: 3
Guess Col: 0
Congratulations! You sank my battleship!

Please post code using the </> icon that appears in the menu bar of the text box you are typing in. Flat (unindented) code is not Python, is hard to read, and cannot be copied and pasted for testing.

Ok

``````from random import randint

board = []

for x in range(0, 5):
board.append(["O"] * 5)

def print_board(board):
for row in board:
print " ".join(row)

print_board(board)

def random_row(board):
return randint(0, len(board) - 1)

def random_col(board):
return randint(0, len(board[0]) - 1)

ship_row = random_row(board)
ship_col = random_col(board)
print ship_row
print ship_col
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"
else:
print "You missed my battleship!"
board[guess_row][guess_col] = "X"
print_board(board)

``````

It appears to work fine: You need to have a loop to keep it going:
One way to do it (in pseudocode):

``````won = False
while not won:
input guesses
if a winner:
print message
won = True
else:
print message
set "X"
print board
# Output (none of your actual code changed, except for adding while loop):
O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
1
1
Guess Row: 3
Guess Col: 4
You missed my battleship!
O O O O O
O O O O O
O O O O O
O O O O X
O O O O O
Guess Row: 2
Guess Col: 3
You missed my battleship!
O O O O O
O O O O O
O O O X O
O O O O X
O O O O O
Guess Row: 1
Guess Col: 1
Congratulations! You sank my battleship!
``````
1 Like

Yeah sorry it did start to work for me in the end. However i did just copy your code and put it at the end of mine but it is not working. It said syntax error for ‘‘input guesses’’ so i removed that and it said syntax error for ‘’ if a winner’’

#output

``````File "python", line 41
if a winner:
^
SyntaxError: invalid syntax
``````

Thanks for your help btw i appreciate it.

2 Likes
``````File "python", line 46
set "X"
^
SyntaxError: invalid syntax
``````

you can’t just copy paste pseudo code and expect it to work

pseudo code is simply english describing code, its not valid code syntax

1 Like

Yes, that was “pseudocode”, meaning that it is supposed to show the logic, but leave the actual coding to you. For instance, “if a winner” would be (from your original posting):

``````if guess_row == ship_row and guess_col == ship_col:
``````

… and "set “X” would be:

``````board[guess_row][guess_col] = "X"
``````
2 Likes