# Example of input verification # Can you write a function now that checks for a VALID answer. # FOr example, in the menu below valid answers are a number from the list: # [1, 2, 3]? #def getValidNumber(question, validAnswer): def getNumericAnswer(question): while(True): x = raw_input(question) # is it all digits if x.isdigit(): return int(x) else: print "There were other things in there... give me all numbers" def getDecimalAnswer(question): # All numbers and one decimal ### Two decimals == bad ### any other char == bad while (True): x = raw_input(question) stList = x.split(".") if len(stList) > 2: # There are too many decimal points! print "Error, you have more than one decimal." elif len(stList) == 1: # There are no decimals in the input, check for non-numeric digits st = stList[0] if st.isdigit(): return float(st) else: print "There are non-numeric digits in your number!" else: # There is one decimal point, check each substring as a number if stList[0].isdigit() and stList[1].isdigit(): # We're good return float(x) else: print "There are non-numeric digits in your number!" def main(): print """ Main Menu 1. Call home and ask for money 2. Go to Atlantic City 3. Use your Python skill to get a job """ ans = getNumericAnswer("What do you want to do: ") print "The answer was %d " %(ans) ans = getDecimalAnswer("What is pi? ") print "Pi is %f" %(ans) main()