This is not an appropriate use of KeyError (it should be used to search in a speaker or similar situations), but if this is what you were asked to do, try something like this
def prompt_thing(): s = raw_input("Please enter the key word you want to use: ") if s == '' or not s.isalnum(): print("The key you entered is invalid. Please try again") raise KeyError('non-alphanumeric character in input') return s s = None while s is None: try: s = prompt_thing() except KeyError: pass
source share