Where is the error?


#1



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


Shows error message : "Make sure to enter a col and row that is on the board!"


As per my knowledge, the code should work fine. Moreover the incorrect guess numbers are not replaced by "X".


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(1, len(board))

def random_col(board):
    return randint(1, len(board))

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!"
    guess_row="x"
    guess_col="x"
    print_board(board)


#2

Your error message mentions user input, so relevant information is what user input you gave it when getting that error message


#3

i always gave an input lying between 0 and 5 for both rows and columns. Still it shows the error.


#4

so 1 2 3 4 only (between)? Or including both ends? That would be 0 1 2 3 4 5 which is 6 rows/cols when there are only 5

You're re-using the variables where the guesses are stored, so they no longer refer to what their names suggest that they refer to


#5

to be on a safe side i enter 3 and 3. it still shows the error. what now?


#6

You overwrote the input, so those variables no longer refer to coordinates that are within the board


#7

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