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


#1

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

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

Help?

EDIT:

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


#2

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.


#3

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


#4

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.


#5

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)

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

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

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


#6

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.


#7

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


#8

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


#9

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


#10

If you have yet to figure it out.

Do this in the python interpreture.

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

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:
        try:
            guess = int(raw_input("Enter a number 1-%s" % len(board))) - 1
            if 0 < guess <= len(board):
                valid = True
                return guess
            else:
                print("You failed to make a valid entry!")
        except ValueError:
            print("You failed to enter a number!")

EDIT:

Oh and adjust your guess.


#11

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


#12

That was my mistake! :confounded: