# function with default value # call function with named arguments # let's start by writing a function that takes price of a purchase # and compute the recommended total pay (= price + tip) # first, let pick a default tip (in percent) # def tip_total(price): # """ # compute recommended total pay # # :param price: price of a purchase # :return: total: total you should pay (including price and tip) # """ # # percent = 15 # # tip = price * (percent / 100) # total = price + tip # return total # # print("1. You should pay " + str(tip_total(30))) # what if we want to make it flexible -- allow the caller to specify the tip percent # that is, the caller will call a function and passes 2 things: price and tip percent # print("2. You should pay " + str(tip_total(30, 20))) # now running this result in error -- trying to pass 2 things but the function expects to receive 1 thing # to solve this, modify the function definition # also comment out percent = 15 # def tip_total(price, percent): # """ # compute recommended total pay # # :param price: price of a purchase # :return: total: total you should pay (including price and tip) # """ # # # percent = 15 # # tip = price * (percent / 100) # total = price + tip # return total # # # print("1. You should pay " + str(tip_total(30))) # now, this function call causes an error -- give 1 but expect 2 # print("2. You should pay " + str(tip_total(30, 20))) # this function call works -- give 2, expect 2 # How should we fix the top_total() function such that it allows a caller to # - pass 1 argument and # - pass 2 argument # The solution is to use a default value # let's modify the function definition # by assigning default values to parameters # note: all default-value parameters must come after non-default-value parameters # def tip_total(price, percent=15): # assume default tip percent is 15 # """ # compute recommended total pay # # :param price: price of a purchase # :return: total: total you should pay (including price and tip) # """ # # tip = price * (percent / 100) # total = price + tip # return total # # # caller passes 1 argument, leaving the remaining argument with default value (i.e., optional argument) # print("1. You should pay " + str(tip_total(30))) # # # caller passes 2 arguments # print("2. You should pay " + str(tip_total(30, 20))) # let's add more parameters # imagine, we want to set a max tip to 100 # that is, if the computed tip is greater than 100, pay max tip of 100 # now, modify the function definition def tip_total(price, percent=15, max_tip=100): # assume default tip percent is 15, max_tip is 100 """ compute recommended total pay :param price: price of a purchase :return: total: total you should pay (including price and tip) """ tip = price * (percent / 100) total = price + min(tip, max_tip) return total print("1. You should pay " + str(tip_total(30))) print("2. You should pay " + str(tip_total(30, 20))) # call with positional arguments # let's call a function and see if max tip is handled properly print("3. You should pay " + str(tip_total(400))) print("4. You should pay " + str(tip_total(1000))) # max tip is used instead of the computed tip print("5. You should pay " + str(tip_total(1000, max_tip=300))) # use named argument to overwrite max tip to 300 print("6. You should pay " + str(tip_total(percent=25, price=1000, max_tip=300))) # use named arguments, not rely on positions of arguments print("7. You should pay " + str(tip_total(percent=25, price=1000, max_tip=200))) # use named arguments, not rely on positions of arguments