What wrong is in my code?


#1

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

My code:

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)

Consol output:

O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
Guess Row: 5
Guess Col: 2
4
3
You missed my battleship!
Traceback (most recent call last):
File "python", line 33, in
IndexError: list index out of range

Error: "There seems to be an error in your code, see the console window for the error message!"

Please, help me. I don't understand what wrong is in my code.


#2

nothing it would seem, but 5 is a invalid entry (which your code is not checking for, not yet), don't forget lists are zero indexed based (counting start at zero)

so the indexes are: 0, 1, 2, 3, 4


#3

instead of board[guess_row][guess_col] = "X" , use board[guess_row-1][guess_col-1] = "X"


#4

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