8. ...and seek! (SCT error)


I’m stuck on exercise 8 of Battleship!, ie. the one where we take inputs from the user.

My code (included below) outputs the following error: Traceback (most recent call last):
File “python”, line 22, in
ValueError: invalid literal for int() with base 10: ‘SCT’

I can see no obvious problems with my code, so any help would be greatly appreciated. I’ve not come across this issue before, so I’m at a loss.

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)

# Add your code below!
guess_row = int(raw_input("Guess Row: "))
guess_col = int(raw_input("Guess Col: "))


what do you enter when prompted for input by raw_input? It should be integers, otherwise you get an error


A post was split to a new topic: 8. and seek


I get the error before I enter anything.


Same here. After I press “Run” it shows:
Guess Row:Traceback (most recent call last):
File “python”, line 22, in
ValueError: invalid literal for int() with base 10: ‘SCT’

It seems to go through the raw_input without any command because even getting the answer from the source, it still happens.


your reply is a low quality reply, please read this topic:

Then make a new topic which of better quality so we can actually help you

@vantonini, did you read my reply:

You didn’t answer what you enter into the prompt it should be integer, if you don’t, int() will throw an error that the input can’t be converted to integer


valkyria said, we did nothing. It happens after pressing “Run” and before enter anything.


multiply people seem to have this problem, but i am not able to reproduce it, try this code:

     guess_row = int(raw_input("Guess Row: "))
     guess_col = int(raw_input("Guess Col: "))
     guess_row = 2
     guess_col = 2

see what happens.


this is weird…I went back to the exercise and the error didn’t occur. Could it be a connection problem because it happened around the same time that valkyria reported her problem.


so a simply page refresh should solve the problem


If replying to what you asked is a low quality reply, I do not know what constitutes a good reply. The same information in 250 words? Like I said, I was unable to enter anything, it did not actually prompt before throwing the error, hence I could not enter either an integer nor anything else. Refreshing did not help at the time either.

Trying the try-except-structure you proposed in another comment resulted in the same error at first, again at the guess_row-line, however it seems to have worked itself out now, for some reason. Presumably connection-related as speculated.

Edit: I’ll clarify, I did not refresh to get it to work. I refreshed, got the error, came here to type the reply, gave it another try without refreshing or the connection timing out, succeeded.

Python raw_input causes error with no chance to input anything
Your own while / else