Danger, Will Robinson!


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

# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
    print "Congratulations! You sank my battleship!"
    print "You missed my battleship!"
    board[guess_col][guess_row] = "X"

Need help. When my input is equal numbers, for example, 4 and 4, the code works. But, for example, if my input is 2 and 4, i get this message: Oops, try again. It looks like your game does not change the O to a capital X for an incorrect guess.


Looking for something?


It appears that you simply mixed the row and column. :slight_smile:


actually that'd be (y, x) because the first value/index is which sublist, and each sublist is a row

the x-axis on a coordinate system is the one that goes towards the right

the nth row is at height n (each row adds height), and height is the y-axis, so (row, col) is (y, x)


You're right. Should've thought about it myself more before posting.


Well it is totally confusing, (I avoid using y/x as names it always turns bad)


Just a guess, but you might want to change your last line to

print print_board(board)

the way you have it now only ends the loop, but adding another print allows the entire grid to be reprinted.


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