12. Bad Aim ERROR : list assignment index out of range. Why, whats wrong?


#1


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

if i give input as more than 5 for either row or column i get an error instead of 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: 1
Guess Col: 8
Traceback (most recent call last):
File "python", line 24, in
IndexError: list index out of range

here is the screenshot plz take a look



I have been searching for 3 hours now and still couldnt find a solution. What am i missing to see??
What am i overlooking?
Why am i getting this error, and what is wrong with my code. Its starting to get frustrating

following is my code, also plz check the screenshot that i have uploaded above

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:"))
board[guess_row][guess_col] = "X"
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!"
    print_board(board)
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!"
        print_board(board)


#2

That is the line that is causing the error. It should not be above the conditionals, but below the Not in the ocean check somewhere (around line 37).


#3

Does that mean that my past three exercises were wrong???
because this is the only how i coded past three of them and it evaluated correct and i passed the assignment.

ok can you help me figure out, exactly where that "somewhere" lies?, because it will help save lot of misconceptual understandings while i try several other combinations. It saves time and misconceptions it really does.
Where to spot that 'somewhere'

Your response is much appreciated


#4

ok here is the update, this edit of the code of mine works perfectly
Thanks to your advice

here, the below code correction, where i commented the previous appendage of "X" to happen at a later stage. I also was able to figure out where that 'somewhere' would be. I understood the logic now,

(correctly working code)

ship_row = random_row(board)
ship_col = random_col(board)
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))
# board[guess_row][guess_col] = "X"
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!"
    board[guess_row][guess_col] = "X"
    print_board(board)
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!"
        board[guess_row][guess_col] = "X"
        print_board(board)

> But still the 1st question remains.

> Was i doing the past three assignments wrong if that appendage statement was at wrong place????


#5

Just leave it at line 37ish, and remove the one on line 31ish.

Could be that the SCT was not checking that closely.


#6

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