Not Again!


#1

Replace this line with your code.

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

Hi, I keep getting a message saying invalid syntax with an arrow pointing to the c in the last guess_col. I've literally copied and pasted others' code on this forum and nothing has worked. Help would be much appreciated.
Thanks!
`


#2

does the order of the code make sense? Your current order:

  1. mark incorrect guess on board with a X
  2. check if the guess is on the board
  3. again mark incorrect guess
  4. check if the user hasn't already guessed that one

#4

I think codecademy messed up. Because we cant check for a second guess being the same as the first if they dont have a option to input a 2nd set of coordinates


#5

we can't through everything at once at learners, you will later add a loop to this program


#6

you need two equal signs not the one when elif board[guess_row][guess_col] = "X"


#7

Tried two == but it doesn't change anything. Still claiming that line has an error in it, though, so I don't think it has to do with order.


#8

please post an updated version of your code


#9

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

#10

so you first mark the guess as a misser, then you check if the guess is on the board or already guessed? That doesn't sound very logic to me


#11

which line(s) are you referring to?


#12

all of them, i recommend you to look through your game flow


#13

@stetim94 I get what you mean and I think it makes much more sense to ask if the values a battleship, have you already tic this place and last if it is out of range ...but I have an other problem.
here the code (only the if/elif/else because the rest is already given):

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

It works fine if I type in something within the range and in the lesson before it already works if I put in some values out of range - but now not. It stopped with the line "elif ..."

and shows
"Traceback (most recent call last):
** File "python", line 31, in **
IndexError: list index out of range"

Where is my error?
Many thanks!


#14

currently, you check if the coordinates are already guessed:

elif board [guess_row] [guess_col] == "X":

before you check the shot is even in the ocean. This means that for invalid coordinates, you still access the list (already guessed) before checking if the coordinates are valid, it should be:

if hit:
   you sank my battle ship
else:
   if not in ocean:
      outside ocean
   elif already guessed:
      you already guessed this one
   else:
      you missed my battle ship
      update board

does this order make sense?


#15

oh - nasty mistake - you are right! Thanks a lot!


#17

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