how to use redefined function or we have to define again (W3D2 questions3 part 4)

W3D2 (question no 3) part 4 not able to understand


The question has 5 parts :

  1. Hotel_cost (takes days as an argument)
  2. Plane_ride_cost (takes city as an argument)
  3. Rental_car_cost (takes days as an argument)
  4. Trip_cost (takes days and city as an argument)
  5. Trip_cost with spending money (takes days, city and spending money as an argument)

So, for hotel_cost you have define function with days as input argument, which is not depended on city it is calculated by given formula.

def hotel_cost(days):
    total_hotel_cost = 140*days
    return total_hotel_cost

Now, for plane_ride_cost you need city as input argument so you have to use if-else condition here.

def plane_ride_cost(city):
    if city == "Charlotte":
        final_plane_ride_cost=183
    elif city == "Tampa":
        final_plane_ride_cost=220
    elif city == "Pittsburgh":
        final_plane_ride_cost=222
    elif city == "Los Angeles":
        final_plane_ride_cost=475
    return final_plane_ride_cost

For rental_car_cost you need days as input because rental car cost only depends on days as suggested in question.

def rental_car_cost(days):
    rental_car_cost = 40*days
    if days >= 7:        
        discount_cost = 50
        total_rental_car_cost = rental_car_cost-50
    elif days >=3 and days <= 7:
        discount_cost = 20
        total_rental_car_cost = rental_car_cost-20
    return total_rental_car_cost

Now for Trip_cost you need 2 arguments i.e. days and city, so for this you already created functions above, you just need to call those functions in Trip_cost function.

def trip_cost(city,days):
    sum = rental_car_cost(days)+plane_ride_cost(city)+hotel_cost(days)
    return sum

Now in last you only need to modify Trip_cost with addition of one more argument of spending money, take it as a default value as 0.

def trip_cost(city,days,spending_money=0):
    sum = rental_car_cost(days)+plane_ride_cost(city)+hotel_cost(days)+spending_money
    return sum