11) Row and Col not on board


#1


When entering "Guess Row": 1 and "Guess Col" 5, an codecademy error returns:
"Oops, try again. Make sure to enter a col and row that is on the board!"


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[0]))

ship_row = random_row(board)
ship_col = random_col(board)

# For testing purposes
#ship_row=5
#ship_col=5

try:
    guess_row = int(raw_input("Guess Row:"))
    guess_col = int(raw_input("Guess Col:"))
except NameError:
    guess_row = int(input("Guess Row:"))
    guess_col = int(input("Guess Col:"))
# For debugging purposes
#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-1][guess_col-1] = "X"
    print_board(board)


Entering "Guess Row:" 1 and "Guess Col:" 5, I get the expected output:

Guess Row: 1
Guess Col: 5
You missed my battleship!
O O O O X
O O O O O
O O O O O
O O O O O
O O O O O

but then codecademy returns an error:
"Oops, try again. Make sure to enter a col and row that is on the board!"

Can someone help me figure this out?


#2

lists are zero index bases, so the indexes are: 0, 1, 2, 3 and 4. so 5 is indeed not on the board


#3

I see, I changed my indexes to match "human" numbering (non-indexed? IDK the termonology) as opposed to changing the human guesses to indexed numbers


#4

since you are working in a programming language it is easier to adjust the human input then trying to change things like list indexes


#5

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