Not Again!


#1



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


The code keeps saying list index out of range. Please, someone help!


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


#2

this code:

elif board[guess_row][guess_col] == "X":
     print "You guessed that one already."

is misplaced. You have an if statement to handle correct guesses, and else when user guesses wrong:

// user guesses correctly
if guess_row == ship_row and guess_col == ship_col:
     print "Congratulations! You sank my battleship!"
// user guesses wrong
else

in the else clause which handles the incorrect guesses, you should have the different cases for the possible wrong guesses (if -> not on board, elif -> already guessed, else -> just missed)


#3

Hi @jubbjubb123,

This line:

        [guess_row][guess_col] = "X"

Should be:

        board[guess_row][guess_col] = "X"

Hope this helps!


#4

The elif statement has to be nested under the else, not a separate statement altogether. Is that right?


#5

The elif shouldn't be nested in the else statement, but rather before. To pass the exercise, you should have an if, elif, then else with a nested if / else.

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

While this got me past the lesson, I encountered issues during the subsequent debug with the nested if function. I had to rearrange the items as @stetim94 advised: if = correct guess; elif = guess out of range; elif = duplicate guess; else = incorrect guess.

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

So far, I haven't had any issues with this while debugging! Hope this helps! :slight_smile:


#6

actually, the elif should be nested inside the else:

// coreccut guess
if guess_row == ship_row and guess_col == ship_col:

// incorrect guess
else:
     // different cases for handling incorrect cases
    // not on board
    if guess_row not in range(5) or guess_col not in range(5):
    // guess already guessed
    elif board[guess_row][guess_col] == "X":
    // player missed battle shipe
    else:

#7

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