13/19 Not Again! and 15/19 Play It, Sam


#1

Hi! I was wondering why my code ends up printing “You guessed that one already.” for inputs that I have not previously inputted.

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)
  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_board(board) 
O O O O O
O O O O O
O O O O O
O O O O O
O O O O O
Guess Row: 1
Guess Col: 3
You missed my battleship!
O O O O O
O O O X O
O O O O O
O O O O O
O O O O O
You guessed that one already.

#2

I fixed it! Though, I’m actually not sure as to why the reason my results came out above. I just reformatted the code. If anyone could still explain to me why my code above was wrong, I would appreciate it.

if guess_col == ship_col and guess_row == ship_row:
  print "Congratulations! You sank my battleship!"
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_board(board)

Edit: I realized afterwards while continuing the exercise that this code doesn’t allow for the X to show in the console after you make an incorrect guess. It still looks like a 5x5 grid of Os after each guess, but the rest of the game continues on just fine.
Is there a reason for this?
Thank you!


#3

why would we re-render something while there has been no change?


#4

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