#---------------------------------------------------------------------- # February 11, 2019 #---------------------------------------------------------------------- #********************************************************************** # Task #1: Write a function that asks the user to enter a number # that is positive or zero, and if not continuously asks until # they do. Return the correctly entered number. #********************************************************************** #---------------------------------------------------------------------- # Solution #0: Function kind of works, as long as the user behaves # and enters a positive number on either the first or second try. # # How to Call: X = GetAPositiveNumber0() # # Problem: Function only asks ***once*** if the number is negative, # so the user could enter a negative number the second time that is # returned. FAIL. Function does NOT work as intended. #---------------------------------------------------------------------- def GetAPositiveNumber0 (): N = float(input("Please enter a non-negative number --- ")) if N < 0.0: N = float(input("Please enter a non-negative number --- ")) return N #---------------------------------------------------------------------- # Solution #1: Function does everything correctly by using a while # to continuously ask for a new number as long as it is entered as # a negative. # # How to Call: X = GetAPositiveNumber1() # # Problem: The message text is typed in twice. If it needs to be # changed, it has to be changed identically in two different places. #---------------------------------------------------------------------- def GetAPositiveNumber1 (): N = float(input("Please enter a non-negative number --- ")) while N < 0.0: N = float(input("Please enter a non-negative number --- ")) return N #---------------------------------------------------------------------- # Solution #2: The text message is saved as a local variable and that # variable is used where needed. If the message must be changed, # there is only one place that needs editing. # # How to Call: X = GetAPositiveNumber2() # # Problem: The N = float(input(Message)) statement still appears twice. #---------------------------------------------------------------------- def GetAPositiveNumber2 (): Message = "Please enter a non-negative number --- " N = float(input(Message)) while N < 0.0: N = float(input(Message)) return N #---------------------------------------------------------------------- # Solution #3: By establishing N as an illegal value first, the code # forces the while-loop to go at least once, getting a new value for N. # # How to Call: X = GetAPositiveNumber3() # # Problem: Changing the message means changing the code. (This has # been a problem all along; we are just now getting to addressing it.) #---------------------------------------------------------------------- def GetAPositiveNumber3 (): Message = "Please enter a non-negative number --- " N = -1 while N < 0.0: N = float(input(Message)) return N #---------------------------------------------------------------------- # Solution #4: Now the Message variable is not established inside the # function, but its value is passed in from outside. The call to # the function now *must* give it a string as an actual parameter. # # How to Call: X = GetAPositiveNumber4("Please enter a value --- ") # X = GetAPositiveNumber4("I want a positive --- ") # X = GetAPositiveNumber4("You are a kumquat --- ") # # Problem: You have to always give the parameter a value, even if it # is the same as all other calls throughout your program. #---------------------------------------------------------------------- def GetAPositiveNumber4 (Message): N = -1 while N < 0.0: N = float(input(Message)) return N #---------------------------------------------------------------------- # Solution #5: The value of Message can still be passed in as a custom # string, but it can be omitted and if so the default value will be # used. # # How to Call: X = GetAPositiveNumber5("Please enter a value --- ") # X = GetAPositiveNumber5("I want a positive --- ") # X = GetAPositiveNumber5("You are a kumquat --- ") # X = GetAPositiveNumber5() # uses the default string # #---------------------------------------------------------------------- def GetAPositiveNumber5 (Message="Enter a positive number --- "): N = -1 while N < 0.0: N = float(input(Message)) return N #********************************************************************** # Task #2: Write a function that accepts a student score as a parameter # and prints the corresponding grade (print an "A" for scores >= 90, # print a "B" for scores >= 80, etc.) #********************************************************************** def PrintGrade (Grade): if Grade >= 90: print ("A") else: if Grade >= 80: print ("B") else: if Grade >= 70: print ("C") else: if Grade >= 60: print ("D") else: print ("F") return #---------------------------------------------------------------------- # Notice the indentation, and that in each case the else is lined up # with its matching if. # # Notice that the function returns without specifying a return value. # Since this function prints its results on the console, there is # nothing to return to the code that called it. In contrast, in all # of the GetAPositiveNumber variants, the final correct value entered # by the user was explicitly returned. # # This illustrates two types of functions: (1) those that compute # something and returns a value, and (2) those that do some action # but have nothing to return. # # Final observation: The if-else-if-else... network in PrintGrade # is fairly large and cumbersome. Think about what it would look # like if it had to deal with UMass A-, B+, B-, C+, C-, and D+ grades! # IS THERE A WAY TO MAKE THIS BETTER? (See next lecture...) #----------------------------------------------------------------------