14. Error when input is not an integer



So I'd like to be able to have an input that is NOT an integer, for instance: q and have my program print out a response such as:
"That was not an integer!"

I'm finding it really hard to get python to check if the input was an integer or not. Here is my code:

from random import randint

` 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:"))

val = int(guess_row)
except ValueError:
print("That was not an integer!")

guess_col = input(raw_input("Guess Col:"))

val = int(guess_col)
except ValueError:
print("That was not an integer!")

if guess_row == ship_row and guess_col == ship_col:
print "Congratulations! You sank my battleship!"

elif board[guess_row][guess_col] == "X":

print "You guessed that one already."

if guess_row not in range(5) or guess_col not in range(5):
print "Oops, that's not even in the ocean."

    print "You missed my battleship!"
    board[guess_row][guess_col] = "X"



I think that if your program didn't throw an error at guess_row = int(raw_input("Guess Row:")), then you already know that you got valid input, so your exception comes too late.


@eveat: It's not that his exception came to late, it's just that nothing wrong happened when he tried to convert the input into a string so no error message was thrown.

@megarockstar69599: If you want to use try-except, which I think it's a good idea, make sure to allow the user to inform the value(s) again so he/she will get the expected outputs from it.
Because a stubborn user my put the incorrect values and expect your code to work, so allow this user to input as many values he/she wants until he/she gets it right.
Is there a need to work like this? No but it's a good practice!



What I meant is that if there is invalid input the error has to be caught at guess_col = input(raw_input("Guess Col:")) already.

I moved that line into the try clause and the program now prints "That was not an integer!" to the console, when the input is invalid.

guess_row = int(raw_input("Guess Row:"))

    val = int(guess_row)
except ValueError:
    print("That was not an integer!")

For input >hello this will give you ValueError: invalid literal for int() with base 10: 'hello'

    guess_row = int(raw_input("Guess Row:"))
except ValueError:
    print("That was not an integer!")

For the same input this prints "That was not an integer!".


@eveat: If you had the guess_row inside or outside the try/except, but not using the int() function with it since it would cause an error, it would work as well BUT it's better to check already inside a try/except while doing all the "transformation" of data type so the program can continue to be executed and the user won't face an ugly error message that wasn't treated by the programmer.


Thanks so much! I agree with what you're saying and it makes lots of sense. The next step I suppose is to allow the player to have as many inputs as they want UNTIL they have the right input.


Great, thanks so much, I've used @eveat's code with the guess_row inside the try/except. The program doesn't continue though and I get an error message thrown at me because now there is no valid input. I'm assuming that I just loop the try/except function until the player inputs an integer?


How do I allow the user to have as many inputs as he/she wants?


@megarockstar69599: Use a loop instruction such as a while or for.
As long as the user's input is invalid, keep outputing the error message and asking the same to inform again the value(s).

When he/she informs the correct input, leave the looping instruction and follow with the execution of your code.


I got it working! Would you like to take a look?


@megarockstar69599: Sure :slightly_smiling: