#Stage 4: #A function that asks the user to enter a positive integer # and returns it after converting the string the user entered # into an integer. Uses a try/except to detect and react to a non-integer #input, and a while loop to re-ask the user until an integer is received! #It also checkes to make sure that the integer is POSITIVE, and won't #return it until it is! def getPositiveInt(): while True: userInput = input("Enter a positive integer:") #returns a string try: #Try converting to an int! userInt = int( userInput) if userInt > 0: return(userInt) #conversion successful, and positive, return the number! else: print("Sorry, not a positive integer, try again!") except: print("You did not enter a valid integer! Try again!") #Stage 3: #A function that asks the user to enter a positive integer # and returns it after converting the string the user entered # into an integer. Uses a try/except to detect and react to a non-integer #input, and a while loop to re-ask the user until an integer is received! ## def getPositiveInt(): ## while True: ## userInput = input("Enter a positive integer:") #returns a string ## try: #Try converting to an int! ## userInt = int( userInput) ## return(userInt) #conversion successful, return the number! ## except: ## print("You did not enter a valid integer! Try again!") #Stage 2: #A function that asks the user to enter a positive integer # and returns it after converting the string the user entered # into an integer. Uses a try/except to detect and react to a non-integer #input ## def getPositiveInt(): ## userInput = input("Enter a positive integer:") #returns a string ## try: #Try converting to an int! ## userInt = int( userInput) ## return(userInt) #conversion successful, return the number! ## except: ## print("You did not enter a valid integer!") ## return( None) #Return None (of NoneType) instead! ## #Stage 1: #A function that asks the user to enter a positive integer # and returns it after converting the string the user entered # into an integer ## def getPositiveInt(): ## userInput = input("Enter a positive integer:") #returns a string ## userInt = int( userInput) ## return( userInt)