70 likes | 203 Vues
Decisions in Python. Boolean functions. A Boolean function. This is a function which returns a bool result (True or False). The function can certainly work with any type of data as parameter or local variable, but the result is bool .
E N D
Decisions in Python Boolean functions
A Boolean function • This is a function which returns a bool result (True or False). The function can certainly work with any type of data as parameter or local variable, but the result is bool. • It’s a good idea to name a bool function with a name that asks a question “is_Error” or “within_range”. Then the return value makes “sense” – if is_Error returns True, you would think there WAS an error. • There is almost always an if statement in a Boolean function because the function can have two different results, True or False.
Use ONE Return statement • Remember the rule of structured programming which says every control structure has one entrance and one exit. A function is a control structure. • It is tempting to write a Bool function in a form like: def fun1 (x): if x > 0: return True else: return False Please do NOT! There are two exits to this function.
Why are two returns a bad thing? • A common mistake in a function like this is to neglect one (or more) of the possible paths through the function, which means that it is possible to execute the function so that None is returned, not True or False! This is definitely a bug. • Example: def yes_no(ans): if ans == ‘y’ or ans == ‘Y’: return True elif ans == ‘N’ or ans == ‘n’: return False • What if the parameter had a value that was not one of ‘y’, ’Y’,’ n’, ’N’? The function would return None!
How to fix this? • Just use an extra variable to hold the return value until the function is ready to return, then use that variable def yes_no(ans): result = False if ans == ‘y’ or ans == ‘Y’ : result = True return result If result did not get an initial value, the return statement will cause a crash, because of “name not defined”, which tells you that you have an error right there. Of course you can fix it by giving result an initial value of True (or False, depending on your specifications)
A Shortcut • The previous function can actually be written in much shorter form def yes_no (ans): return ans == ‘y’ or ans ==‘Y’ The expression in the return statement must be evaluated before it can return. The relational operators and logical operator will combine to give a bool value – if the parameter had a ‘y’ in it, you get a True – anything else gives a False.
Calling Boolean functions – another shortcut • When you call a Boolean function, you call it as part of another statement (since it returns a value). • The value it returns is a Boolean value, True or False. • If you are calling the function as part of an if statement, for example, you can call it this way: if is_error(p1): print(“something error”) This is the same shortcut described in a previous video. is_error(p1) is replaced by a Boolean value when it returns, so you do not have to compare it to something else, like “== True”.