11/19 Can't make the O change to X


Here is the code i am using:
if guess_col == ship_col and guess_row == ship_row:
print "Congratulations! You sank my battleship!"
print "You missed my battleship!"
board[guess_row][guess_col] == "X"

The code runs normally most of the time (aside from the board remaining the same), but sometimes the console prints out the error

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



In board[guess_row][guess_col] == "X", i just needed to use one = .


I would add a print statement above that line (33) so that I could see what indexes it's trying to access. When it crashes, you'll have that print and you can consider if it's reasonable or not. (If it crashed with an unreasonable index, then look at how you computed the index. If it crashed with a reasonable index, then it means the board doesn't have the right size.


I don't really understand what you said to print above line 33. Although i noticed i missed a [0] in

def random_row(board):
return randint(0, len(board[0]) - 1)

After i fixed that the program stopped having the error i mentioned before, but it still doesn't change the O to X.

And oddly enough the course said i managed to finish that exercise when i managed to correctly guess where the battleship was.

EDIT: it still gives this error when one of my guesses is 5:

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


I get the same error message when I put in 5, I guess it's a list index issue since the index starts off from 0 to 4 anything above 4 will break the code.

That is just my guess though.


Here is my code:

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)


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

if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"
print "You missed my battleship!"
board[int(guess_row)][int(guess_col)] = 'x'

it works, but giving me the below erro, can anyone see the error?r:

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


The problem is that you have board[guess_row][guess_col] with == instead of =.

= means equals something. such as setting a value to a variable.

== is comparative.


Getting an invalid syntax error message regarding the "else" on line 31:
File "python", line 31
And here is my code (my preview didn't display proper indentation so the simplest soln was to screengrab):


Adjusted the "else" on line 31 indents and ran the following, doesn't feel right but I got a pass:


No, thats nothing to do with it. Anybody else?


If you have yet to figure it out.

Do this in the python interpreture.

a = [x for x in range(5)]

Notice that a is equal to a range of numbers from 0 to 4 all included 5 different entries.

Your input takes any number, which can and will lead to this bug, also it failes to adjust the number to the computer scale of 0+ instead of the human scale of 1+.

You have to adjust and limit what they can input.

def ask_xy(valid=False): # You can pass the board length in, but add a parameter if so
    This function returns the player input and checks
    to see if it is within the required range.
    while not valid:
            guess = int(raw_input("Enter a number 1-%s" % len(board))) - 1
            if 0 < guess <= len(board):
                valid = True
                return guess
                print("You failed to make a valid entry!")
        except ValueError:
            print("You failed to enter a number!")


Oh and adjust your guess.


Hey, I noticed that you have a lowercase x.
Try making it upper case


That was my mistake! :confounded: