Stuck error checking guess_row and guess_col variables


#1



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


Hi so I'm trying to error check my inputs and using the suggested method of capturing the user input:

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


if I add anything that isn't an integer then I get the message:

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

Any ideas how to capture and store the input but also test that the entry is an integer?


#2

you could use try and except:

try:
   guess_row = int(raw_input("Guess Row:"))
   guess_col = int(raw_input("Guess Col:"))
except ValueError:
   print "that is not a number"

if you want to prompt the user again, you would have to combine it with a loop


#3

good one! I tried it earlier but I must have screwed it up. Working just fine though. Was about to try taking the raw_input and testing it with isdigit() before converting it to an int but this'll work just fine.

Cheers stetim