Battleship - Try guess_row loop


#1



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


I'm trying to make my code handle non-int variables and repeat the input question if a letter is entered instead of a number, any advice ??

Would like to use a try loop to receive a correct input but I'm not sure how to do it!


try: 
    guess_row = int(raw_input("Guess Row:"))
except:
        print "please enter a valid number"
try:
    guess_col =int(raw_input("Guess Col:"))
except:
        print "please enter a valid number"
.


#2

Hi @scriptpro80910,

There are various ways you can do that. An example for getting the row input is ...

while True:
    try:
        guess_row = int(raw_input("Guess Row:"))
        break
    except:
        print "Invalid Row"

#3

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