Danger, Will Robinson! It's really a danger!

Continuing the discussion from FAQ: Learn Python - Battleship - Danger, Will Robinson!:

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
2
3
Guess Row: 2
Traceback (most recent call last):
  File "python", line 24, in <module>
ExecTimeoutException: Program took too long to terminate.
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(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: "))        # line 24
guess_col = int(raw_input("Guess Col: "))           # line 25


I’m continuously getting this error message. I also try out the codecademy solution so the problem isn’t with the code. I can give just one input for the row but not for the column. The next line "Guess Col: " doesn’t get printed on the console.
Would anyone help me to give the input for “guess_col:”

If you add a print or something, e.g. the value of guess_row between the two does that get sent to output? Working out where the hang is might be useful to you.

I also use print command. But the console don’t let me give input for the column. The revolving circle icon in ‘Run’ button makes me to wait.

Did the print work? Has the first input actually been passed to int and then assigned to ship_row?

‘print’ didn’t change the console outlook. It was same as before asking for the row input and then I press an integer key (e.g. 2) from my keyboard and then assigned to ship_row.

@16moon, unfortunately the exercise is bugged. You won’t be able to input guesses using raw_input. The functionality on CC’s learning environment isn’t there to support getting user input in this exercise. You can do something like this as a ‘workaround’ in order to complete the project, and then paste your code into a site like https://repl.it/ to test it for real while actually getting user input:

  guess_row = random_row(board) #int(input("Guess Row: "))
  guess_col = random_col(board) #int(input("Guess Col: "))

I commented out the code you should be able to use, and used the existing functions to generate random guesses.

1 Like

Thanks for the suggestion. The commented code worked out.

1 Like

Thanks for the response.

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