Danger, Will Robinson! - Wierd error


#1

I Dont seem to be doing anything wrong, and have even looked up the answer multiple places, and it still won't work.

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

Here is the full console 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: 4
Guess Col: 5
1
0
You missed my battleship
Traceback (most recent call last):
File "python", line 33, in
IndexError: list assignment index out of range

Why is there an error when there shouldn't be one?


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


#2

You use improper indentation in you if/else statement. You use three spaces where you should be using four.

Also, you forgot the ! at the ends of both print "Congratulations! You sank my battleship" and print "You missed my battleship...


#3

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

Your last print is indented. It shouldn't be.


#4

...? What are you talking about? That print statement should not be outside the else statement. What you just did would screw up the function in later exercises.

Besides, that's not even the proper syntax for that statement, you would call print_board(board) and you would call it inside the else statement.


#5

I tried that at first, except it didn't work. After I removed the indent, it did.


#6

You must have done something wrong elsewhere. Because doing it the way I just described works perfectly fine. You were probably receiving the error because you use print board instead of print_board(board).


#7

Thanks! It's odd that it didn't automatically use 4 spaces when I was typing it (because it usually does) so I didn't notice it.
But what is strange is that I used other people's code that supposedly worked, but didn't for me.


#8

Hi @justinellis,

After this interaction ...

Guess Row: 4
Guess Col: 5

... this happened ...

File "python", line 33, in 
IndexError: list assignment index out of range

This must be line 33 ...

    board[guess_row][guess_col]="X"

That line of code is fine, but there is no column 5. Your guess was out of range, and led to the IndexError when execution reached that line.

In a later exercise, you will write some code that catches a guess that is off the board to prevent an IndexError.


#9

I tried different variables like 2:3 and 0:1 etc.
still didn't work at the time.
My given code was not the best example (as like aquaphoenix said, I didn't have "!" in the string, when I originally did but forgot it when I rewrote everything, for example) and how I showed a row of 5 in my guess from being sloppy. Thanks for the help though. *Edit but wait, there is 5 rows and columns?
1--2--3--4--5
O O O O O 1
O O O O O 2
O O O O O 3
O O O O O 4
O O O O O 5

unless it reads them in 01234... etc. like in lists?


#10

Yes, the coordinates range from 0 to 4, inclusive. Therefore, this led to an IndexError ...

Guess Row: 4
Guess Col: 5

Another problem is what @aquaphoenix17 pointed out here: https://discuss.codecademy.com/t/danger-will-robinson-wierd-error/80672/2?u=appylpye
For example, you had ...

    print "You missed my battleship"

... instead of ...

    print "You missed my battleship!"

#11

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