Checking if the input is an integer


#1



https://www.codecademy.com/en/courses/python-beginner-en-4XuFm/2/2?curriculum_id=4f89dab3d788890003000096

If the input to the battleship game is a non-integer value, you get the error:

Traceback (most recent call last):
File "python", line 31, in
ValueError: invalid literal for int() with base 10: 'l'


The input from the raw_input() function is always taken to be a string, so we use the int() function to convert it to an integer. That's what gives the error when trying to convert a letter into an integer, for example. I'm wondering if there is any way to check the input and provide a message to the effect of "Please enter a valid integer" if the input is in fact a non-integer, before attempting to convert the input to an integer with int() and getting the console error.

Here is the code of interest:

for turn in range(4):
    print "Turn", turn + 1
    guess_row = int(raw_input("Guess Row:")) - 1
    guess_col = int(raw_input("Guess Col:")) - 1
    
    if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sunk my battleship!"
    else:
        if (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
            print "Oops, that's not even in the ocean."
        elif(board[guess_row][guess_col] == "X"):
            print "You guessed that one already."
        else:
            board[guess_row][guess_col] = "X"             
            print_board(board)            
            print "You missed my battleship!"
        if turn == 3:
            print "Game Over"


#2

Consider this example taken from the Errors and Exceptions documentation:

while True:
     try:
         x = int(raw_input("Please enter a number: "))
         break
     except ValueError:
         print "Oops! That was not a valid number. Try again..."

Also, we can make the code a little more fluid if all the clauses are elif, with only one else at the end. Essentially, go back to the beginning where we had only if .. else, and every lesson after that one, insert the new elif before the else.

    if guess_row ...

    elif guess_row not in range(5) ...

    elif board[guess_row][guess_col] ...

    else:
        # you missed my battleship

Make sense?


#3

This is exactly what I was looking for! It handles the error just how I wanted. Here is what my code looks like after adding the error exception code and using more of the elif conditionals. Thanks a lot for your help my friend!

for turn in range(4):
    print "Turn", turn + 1
    while True:
        try:
            guess_row = int(raw_input("Guess Row:")) - 1
            guess_col = int(raw_input("Guess Col:")) - 1
            break
        except ValueError:
            print "Oops! That was not a valid integer. Please try again."
    
    if guess_row == ship_row and guess_col == ship_col:
        print "Congratulations! You sunk my battleship!"
    elif (guess_row < 0 or guess_row > 4) or (guess_col < 0 or guess_col > 4):
        print "Oops, that's not even in the ocean."
    elif(board[guess_row][guess_col] == "X"):
        print "You guessed that one already."
    else:
        board[guess_row][guess_col] = "X"             
        print_board(board)            
        print "You missed my battleship!"
        if turn == 3:
            print "Game Over"