Lecture 5 Conditionals and functions CS 51 P




















- Slides: 20

Lecture 5: Conditionals and functions CS 51 P January 31, 2020

Recap what's your favorite positive integer? 13 me too! what's your favorite positive integer? 20 mine is 13 x = int(input("pos int? nt")) if x == 13: print("me too!") else: print("mine is 13")

nested if-statements x = int(input("pos int? nt")) if x == 13: print("me too!") else: if x < 13: print("that's less than mine") else: print("that's more than mine")

x = int(input("pos int? nt")) if x == 13: print("me too!") else: if x > 30 or x < 10: print("mine is 13") else: if x == 19: print("19!") else: print("? ") print("!")

if-else, if-else statements x = int(input("pos int? nt")) if x == 13: print("me too!") elif x > 30 or x < 10: print("mine is 13") elif x == 19: print("19!") else: print("? ") print("!")

What about? what's your favorite positive integer? absdfa that's not good input! x = input("what's your favorite " + "positive integer? ") if not(str. isdigit(x)): print("that's not good input") • the condition can be a function that returns a value of type bool

Functions • A function is a named sequence of statements that performs some useful operation • When you call a function, the sequence of statements executes. • When should, and how can, you define your own functions?

Functions • Why define a function? • There's some useful operation that you want to do over and over • How to define a function? header body ++++ ++ ** ++ ++++ def print_logo(): s 1 = (8*'+')+'n' s 2 = '++ ** ++n' print(s 1+s 2+s 1)

def print_logo(): s 1 = (8*'+')+'n' s 2 = '++ ** ++n' print(s 1+s 2+s 1) print_logo()

• What if you want an arbitrary middle? In other words, when you call print_logo() you want the function to ask the user for a character and then you want to print that character in place of the *'s? ++++ ++ ** ++ ++++++++ ++ CC ++ ++++++++ ++ SS ++ ++++

def print_logo(): c = input("character? ") s 1 = 8*'+' + 'n' s 2 = '++ ' + 2*c + ' ++n' print(s 1+s 2+s 1)

Functions • How about a function called good_choice() that asks the user for a positive integer and returns True if and only if the user enters 13? • We want to be able to use the function as follows: if (good_choice()): print("yay") else: print("boo")

def good_choice(): x = int(input("positive integer? ")) if x == 13: return True else: return False if (good_choice()): print("yay") else: print("boo")

def good_choice(): x = int(input("positive integer? ")) if x == 13: return True else: return False if (good_choice()): def good_choice(): print("yay") x = int(input("positive integer? ")) else: Return x == 13 print("boo") if (good_choice()): print("yay") else: print("boo")

def good_choice(): x = int(input("positive integer? ")) Return x == 13 • How would you modify the function good_choice() so that it returns True if and only if: • x is neither 32 nor 64 ? • x has a 3 in the ones digit ? • the user enters two numbers that are both even ?

main() def good_choice(): x = int(input("pos int? ")) Return x == 13 def good_choice(): if (good_choice()): x = int(input("pos int? ")) print("yay") Return x == 13 else: print("boo") def main(): if (good_choice()): print("yay") else: print("boo") if __name__ == "__main__":

More practice • Write a function called asterisks() that asks the user for a positive integer, prints that many asterisks, and then returns the number of asterisks that were printed • Write a main() functionthat calls asterisks() three times and prints the total number of asterisks that were printed

Coding Style • Things that don't affect correctess, but improve readability, maintainability, etc. • Includes: • Using meaningful variable names • Using consistent variable names • Using (consistent) whitespace • Commenting code (consistently)

Commenting • Docstrings • "A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. " (*) • every file should start at the top with a multiline comment that gives the author, date, description of what the code does • every function header should be followed by a multiline comment that describes what the function does and specifies the return type/value (and, eventually, any input parameters) • Comments • describe what your code does and not how it does it • think about what might confuse other people trying to make sense of your code

Example # TODO: implement div_by_2 function def div_by_2(): ''' Asks for an integer and determines whether that number is divisible by 2 : return: bool: True if and only integer is divisible by 2 ''' pass