13/19 Not Again!


#1


https://www.codecademy.com/en/courses/python-beginner-en-4XuFm/1/4?curriculum_id=4f89dab3d788890003000096


The error is File "python", line 38
else board[guess_row][guess_col] == "X":
^
SyntaxError: invalid syntax

Oops, try again. Your code looks a bit off. Check the Hint if you need help! Your code threw the following error: invalid syntax (python, line 38)


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:
    print "You missed my battleship!"
    if guess_row not in range(5) or guess_col not in range(5):
        print "Oops, that's not even in the ocean."
    else:
        board[guess_row][guess_col] == "X"
print_board(board)
else board[guess_row][guess_col] == "X":
print "You missed my battleship!"
print print_board(board)
elif:
board[guess_row][guess_col] == "X"
print "You guessed that already."

Please help


#2

The error is telling you that:
else-statements cannot have a condition.

Here you forgot a condition for your elif.


#3

thank you so much .............


#4

You are welcome!

Post again if you get confused! :slightly_smiling:


#5

i did a ew code and it wouldn't work\

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:
print "You missed my battleship!"
if guess_row not in range(5) or guess_col not in range(5):
print "Oops, that's not even in the ocean."
else:
board[guess_row][guess_col] == "X"
print_board(board)
else:
board[guess_row][guess_col] == "X":
print "You missed my battleship!"
print print_board(board)
elif board[guess_row][guess_col] == "X":
print "You guessed that already."

error
File "python", line 38
else:
^
IndentationError: unexpected indent

File "python", line 38
else:
^
SyntaxError: invalid syntax

please help


#9

Try this it worked for me

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


#10

I am able to pass the lesson with my code the same, but I can't get the "You guessed that one already." message - even when entering in the same guesses. Maybe that will be addressed in one of the next lessons?

Here's my code, not sure how to get the indents to show:

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 guess_row == "X" and 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)


#11

@tyriverag: Your code is not incorrect, in fact is doing just what it has to do but without a loop instruction, which will be used to repeat each instructions over and over again to check for something, it will only check if you guessed it correctly where the ship is.
Try adding a for or while loop, with that code inside of the scope and fixing the identation, and you'll see how it works.

Hope this helps, if not, let us know!


#12

3 posts were split to a new topic: Doesn't get me the go-to next chapter


#13

this is wrong. your guess_row and guess_column never have 'X" sign, the 'X" is on your board. so it has to be
elif board[guess_row][guess_col] == 'X':