14. Test Run - Blank Entry


#1



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


When testing the code, I get an error if I try to leave one of the inputs blank:

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


Is there a simple if statement to test if the raw input is blank and return a new message?


guess_r = raw_input("Guess Row:")
if guess_r == "":
  print "Blank"
else:
  guess_row = int(guess_r)

Update: I started playing around with the above line and it correctly identified if the input is blank. I guess I would need to loop back to the input some how.


#2

ideally, you would do this:

while True:
    try:
        guess_r = int(raw_input("Guess Row:"))
    except ValueError:
        print "Not a integer"
    else:
        break

try except allows you to handle errors


#3

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