#---------------------------------------------------------------------- # (C) Monday, January 29, 2018 -- Dr. William T. Verts # # This third version solves the "duplicate code" problem by writing # a function to be used/called in each case. The GetNumber function is # designed to return a number which is 0 or positive, BUT it has to # tell the user what value is expected. Is it Number #1 or Number #2? # That is the purpose of the parameter Response, which is a placeholder # for a string passed in from the main program. Whatever value of # Response is passed in, that is what shows up during the invocation of # the input functions. That is, the formal parameter Response is used # as an actual parameter to input. In the main program, the strings # "Enter Number #1 --- " and "Enter Number #2 --- " are the actual # parameter values passed in to Response. The value N passed back by # the return statement is saved in N1 or N2, as appropriate. #---------------------------------------------------------------------- def GetNumber (Response): N = input(Response) while (N < 0): print "This is not a positive number" N = input(Response) return N N1 = GetNumber("Enter Number #1 --- ") N2 = GetNumber("Enter Number #2 --- ") A = (N1 + N2) / 2.0 print "The average is: ", A