print("\nPlaying with functions ...\n") def diagnostic(test = 0, content = "diagnostic help"): '''Diagnostic is designed to print useful diagnostic information. The second var is the text to print, default is "diagnostic help". If the first var = 1 then print happens, otherwise not, default is 0''' if test == 1: print(">>>>\t" + content + "\t\t<<<<") def get_int_from_keyboard( show = 0 ) : '''get_int_from_keyboard asks for an integer and returns the input as an integer. If bad input is given then it returns -999. Careful manual checking to see if the first character is a + or - The input var should be an integer which prints diagnostic info if set to 1, default 0''' print("please enter an integer\n") str_input = input( ) diagnostic(show , "/t>>> you gave me " + str_input) ## diagnostic if str_input.isdigit(): diagnostic(show , "it's a non-negative integer") return int(str_input) ## so it was a non-negative integer ## we only get here if we haven't already returned!!! if len(str_input) > 1: ## check to see if it's a negative integer by looking at the first character of the input string firstChar = str_input[0] minusPlus = False # a default value which might change if firstChar == '+' or firstChar == '-': minusPlus = True diagnostic(show , "it's not a +ve int, but it's longish and minusPlus = " +str(minusPlus)) if minusPlus and str_input[1:].isdigit(): diagnostic(show , "it's a pos or neg integer ... " + str_input) return int(str_input) ## so it was a negative integer diagnostic(show , "it's longish but isn't digity after the first character") ## it's neither a positive nor a negative integer :( print("OUCH -- you didn't give me what I wanted!!!! I'm going to return -999") return -999 print("\tabout to demonstrate how default values for functions work ...\n") diagnostic(0,"IO") diagnostic(1) diagnostic(1, "goofy") print("\n\tNow to get into the real program ...\n") show = 0 ## this will hide the diagnostic printouts, set show = 1 to see them compoundMe = get_int_from_keyboard(show) * get_int_from_keyboard(show) print("\n\ttesting the function ... the product = " + str(compoundMe))