May-28-2020, 11:04 PM
The solution is the same solution I would use for solving this with C, create an association between the shipping cost function and the shipping cost name. In Python I might use a dictionary.
def ground_shipping_cost(weight): cost = weight * 4.00 return cost def drone_shipping_cost(weight): cost = weight * 4.50 return cost def premium_ground_cost(weight): return 125 shipping_options = { 'Ground': ground_shipping_cost, 'Drone': drone_shipping_cost, 'Premium Ground' : premium_ground_cost } def method_is_cheapest(weight): option = None min_cost = 0 for name in shipping_options: cost = shipping_options[name](weight) if cost < min_cost or option is None: min_cost = cost option = name return option weight = float(input('Enter package weight: ')) option = method_is_cheapest(weight) cost = shipping_options[option](weight) print(f'Shipping {option} is the cheapest at ${cost:.2f}')