I have a syntax error when running code in actual Python


#1

I have finished Battleship! and was wanting to put my code in my actual Python program. I am using Python 3.5

The line of code I am having a problem with is:
print " ".join(row)
Python is saying there is a syntax error at the quotation marks. I have tried not having a space in between the marks, but that still throws a syntax error.

Can someone please help me understand why it is not working?


#2

it depends what is row? a list or string


#3

Here is the full code:

from random import randint

board= []
for x in range(5):
[tab]board.append(["O"}*5)
def print_board(board):
[tab]for row in board:
[tab][tab]print " ".join(row)#Here is the line of code.

print "Let's play Battleship!"
print_board(board)

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

ship_row=random_row(board)
ship_col=random_col(board)

for turn in range(4):
[tab]guess_row=int(raw_input("Guess Row:"))
[tab]guess_col=int(raw_input("Guess Col:"))

[tab]if guess_row==ship_row and guess_col==ship_col:
[tab][tab]print "Congratulations! You sunk my battleship!"
[tab][tab]break
[tab]else:
[tab][tab]if(guess_row<0 or guess_row>4 or (guess_col<0 or guess_col>4):
[tab][tab][tab]print "Oops, that's not even in the ocean."
[tab][tab]elif(board[guess_row][guess_col]="X"
[tab][tab][tab]print "You guessed that one already."
[tab][tab]else:
[tab][tab][tab]print "You missed my battleship!"
[tab]board[guess_row][guess_col]="X"
[tab]print "Turn", turn + 1
[tab]if turn == 3:
[tab][tab]print "Game Over"

print_board(board)


#4

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)

for turn in range(4):
    print "Turn",turn+1
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))
    
    if(guess_row is ship_row and guess_col is ship_col ):
        print ("Congratulations! You sank my battleship!")
        break
    else:
        if (turn== 3):
            print "Game Over"
            break
        else:
            print("You missed my battleship!")
        
    
    if(guess_row in range(0,5)and guess_col in range(0,5)):
        if(board[guess_row][guess_col]== "X"):
            print("You guessed that one already.")
        else:
            board[guess_row][guess_col] = "X"
    else:
        print("Oops, that's not even in the ocean.")
    
        
print_board(board)

if you want to format your code when you paste it here you have three options

  1. select the code (after you paste it here) and press ctrl + k or
  2. select the code(after you paste it here) and press </> in the tools pane
  3. put this character `

#5

Rydan, thank you very much! It works now.