16/19 - Not enamored of Battleship now


Battleship not erroring per se, but not allowing guess_col entry:

As you can see it is not allowing a guess column entry to be made and immediately cycles through the if/elif/else and returning to the for line. Can someone help me find out why?

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

for turn in range(4):
  print "Turn", turn + 1
  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 sank my battleship!" <br />
    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." )
      print "You missed my battleship!"
      board[guess_row][guess_col] = "X"
    if (turn == 3):
      print "Game Over"


I swear I tried to use a Markdown editor prior to entering my code


Hello :slight_smile:

I swear I tried to use a Markdown editor prior to entering my code

It’s ok, I added code to your post, you can edit it and see how it is done.

It looks like the problem is the input you gave. When Guess Row: showed up what did you type? According to the error message it was RUN and your code expects it to be a numeral value, for example 3.

Execute your code again and play by the rules, try to guess where is the battleship by providing integers from 0 to 4.


Thank you for your help. But, I still don’t know how to enter code edited correctly and don’t know how viewing/editing the post will show me.

BTW: unfortunately I didn’t notice that the screen I entered wasn’t the screen shot that I was speaking about. Please excuse me. I did enter an integer and the system would automatically place the “you missed my battleship” message on the line where I was supposed to enter the col integer. That was my issue. I closed the chrome tab and back in - still erroring. I then started the screen shot etc and the screen refreshed right when I was opening the snipping tool. I didn’t notice it had changed to the screen that you see. I logged out and back in. New chrome tab. Same code.
Now it works fine and I’ve advanced through to the next lesson. This has happened several times. I guess I’ll just log out and back in again each time I get an error that doesn’t make sense.

Thanks again.


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