13/13 Not again!


#1



The question is about storing X in board. For example, Ive typed those numbers: 2 and 2(for row and col accordingly). Program output was: "You missed". Obviousley, If I type such a numbers once more there must be 'X', but its output is same as at the beggining - "You missed"


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)
print ship_row
print ship_col
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))

# Write your code below!
if guess_row == ship_row and guess_col == ship_col:
    print ("Congratulations! You sank my battleship!!!")
    board[guess_row][guess_col] = "X"
    print print_board(board)
else:
    if guess_row not in range(5) or guess_col not in range(5) :
        print ("Oops, that's not even in the ocean.")
    elif board[guess_row][guess_col] == "X":
        print "You guessed that one already."
    else:
        print ("You missed my battleship!")
        board[guess_row][guess_col] = "X"
        print print_board(board)
 
I`m wondering how can I store X in the program? Because this code doesn`t work:
if guess_row not in range(5) or guess_col not in range(5) :
        print ("Oops, that's not even in the ocean.")
    elif board[guess_row][guess_col] == "X":
        print "You guessed that one already."
    else:
        print ("You missed my battleship!")
        board[guess_row][guess_col] = "X"
        print print_board(board)


#2

Then it stands to reason that either the code that places the X there isn't doing its job, or the condition that determines if there's an X there isn't doing its job. ..Or both, point is, that's just two places to look.

..Or maybe the X's move eh? Seems less likely though.


#3

Not sure how you're guessing more than once seeing as your game ends after one turn.


#4

I figured it out: there must be a loop.
Every time when I start the code above, the code starts from the very beginning(with empty memory). If I loop it from "if guess_row == ship_row and guess_col == ship_col : ........" then the code stores X in board.
for turn in range(4):
guess_row = int(raw_input("Guess Row:"))
guess_col = int(raw_input("Guess Col:"))
if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sunk my battleship!"
else:
if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
print "Oops, that's not even in the ocean."
elif(board[guess_row][guess_col] == "X"):
print "You guessed that one already."
else:
print "You missed my battleship!"
board[guess_row][guess_col] = "X"


#5

Btw., Thank you for your reply!