Not again


#1

Getting error when debugging code and have looked through a number of topics with people have the same issue. 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)
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

print ship_row
print ship_col

Write your code below!

if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"
else:
if guess_row not in range(5) or \
guess_col not in range(5):
print "Oops, that's not even in the ocean."
elif board[guess_row][guess_col]="X":
print "You guessed that one already."
else:
print "You missed my battleship!"
board[guess_row][guess_col]="X"
print print_board(board)

Error message:
File "python", line 35
elif board[guess_row][guess_col]="X":
^
SyntaxError: invalid syntax


#2

Do you mind formatting your code like this? How do I format code in my posts?

Just want to make sure indention is not a possible cause.


#3

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)
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))

print ship_row
print ship_col

# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"

else:
    if guess_row not in range(5) or \
    guess_col not in range(5):
        print "Oops, that's not even in the ocean."
    elif board[guess_row][guess_col]="X":
        print "You guessed that one already."

else:
    print "You missed my battleship!"
        board[guess_row][guess_col]="X"

print print_board(board)


#4

ah, I see it now. Your condition for the elif statement has the assignment operator (=) rather than the equals operator (==). A common mistake that just takes some time to get used to the syntax of programming.

This may be that it's broken up, but it seems that the elses are on the same indention line which might lead you to a problem. Same with the board[guess_row][guess_col]="X" that also seems to be a bit off from it's proper indention.
This lesson should show why: I Got 99 Problems, But a Switch Ain't One

Hope this helps! If not, feel free to reply and I'll try to go more in-depth.


#5

Yeah i added in == and am now getting this error message:
File "python", line 37
else:
^
SyntaxError: invalid syntax

So I think you're right on the elses being on the same indention but im not sure how it should be then


#6

I got it and you were right

else:
    if guess_row not in range(5) or \
    guess_col not in range(5):
        print "Oops, that's not even in the ocean."
    elif board[guess_row][guess_col]=="X":
        print "You guessed that one already."
    else:
        board[guess_row][guess_col]="X"
        print "You missed my battleship!"

#7

When it comes to indenting, I like to think of them as a hierarchy. The furthest on the left is the most important and should be looked at first before the statements inside should be read.

if 2 > 3:
    return "I won't be read unless 2 > 3 is True"
elif 10 / 5 == 1:
    if i > 0:
        print "I won't be read unless elif is True and i > 0"
    elif i < 0:
        print "I won't be read unless elif is True and i < 0"
    else:
        print "I will only be read if i == 0"
else:
    if 5 + 1 == 6:
        print "Finally, I'm read because the left-most if and elif are false, ignoring everything inside them, and coming to me, else. The if-statement inside me is also true which lets his be printed."
    elif 3 / 3 == 1:
        print "This elif statement might be true but the previous if statement was already found to be true. After the above if-statement's code is passed, this elif and anyother elif or else on this indention will be skipped until a new if-statement is created on the left-most side."

Tried to show it like this, I know there's a post somewhere that's more brief then this. I'll try to find it and link it.

Hope this helps! If not, feel free to reply and I'll try to go more in-depth.


#8


This post has a more brief representation of indenting that might be more helpful.


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.