Extra Credit


Hey guys,

I'm onto the extra credit, and I'm trying to figure out a way for the code to cope with an incorrect input (letters, or, more commonly, a blank).

I tried adding a line to the IF statements, however, the error is encountered in the line where it takes the int(raw_input("Guess Row: "))

I tried then turning the guess_row and guess_col variables into functions, however, it then won't call that function when I try to run the simulation.

Any ideas or direction would be greatly appreciated :slight_smile:


One approach would be to abstract away the user input to a function that loops over and over until a valid input is received. The program never sees invalid data in the return value.

def user_input(prompt, limit=0):
    if limit:
        err = ""
        while True:
            user = int(raw_input(err + prompt))
            if user in range(0, limit):
                return user
            err = "Must be in range 0 to %d\n" % limit - 1
    return raw_input(prompt)


This will return raw text input with no testing.

user_name = user_input("Enter your name: ")

This will return a number from 0 to height -1 or width -1:

guess_row = user_input("Guess Row: ", grid_height)

guess_col = user_input("Guess Col: ", grid_width)


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