Pull it Together


#1

I keep getting the following error for this activity:

Oops, try again. trip_cost('Charlotte', 4) raised an error: cannot concatenate 'str' and 'NoneType' objects

def hotel_cost(days):
        return 140*days 
        
        
def plane_ride_cost(city):
    if city == "Charlotte":
        return 183
    elif city == "Tampa":
        return 220
    elif city == "Pittsburgh":
        return 222
    elif city == "Los Angeles":
        return 475
        
        
def rental_car_cost(days):
    cost = 40*days
    if days >= 7:
        return cost - 50
        
    elif days >=3:
        return cost - 20
        
    return cost
    
def trip_cost(days, city):
    return hotel_cost(days) +plane_ride_cost(city) + rental_car_cost(days)


#2

Here,

Try passing city as the first argument like this.

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

#3

awesome that fixed it but can you explain to me why the order of the arguments matters?


#4

Because Control Flow.

The way your arguments are arranged is the order in which they can be used and since I put plane_ride_cost(city) first python knows that is the first argument called.


#5

Its like defining a function that adds

def add(x, y):
    return y + x

Do you think the code will run correctly?


#6

how does that not affect the fact that days is being used twice?


#7

Because the number of times an argument is used in a function has no limit. :slight_smile:


#8

But I would always have to call in order. So as soon as I call the second argument I can no longer call the first argument?


#9

Yes, Control Flow is a big part of functional programming. :smile:

If you don't write your code in the right order; your debugging process will be a lot longer.


#10

Hi, I tried reversing the order

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

However when doing so I got a error reading "Oops, try again. trip_cost('Pittsburgh', 5) raised an error: unsupported operand type(s) for -: 'str' and 'int' "

How does this error come about?


#11

Post your code now with the correct order so we can see what else is causing an error. :grin:


#12

I just love u!

I was trying to make this work the whole week!!!

I REALLY thank you! You saved my day, dude!!!! :slight_smile:


#13

Lol you're welcome :slight_smile:


#16

Oops, try again. trip_cost('Los Angeles', 2) raised an error: unsupported operand type(s) for -: 'str' and 'int'

def hotel_cost(nights):
    return 140*nights
def plane_ride_cost(city):
    if city=='Charlotte':
        return 183
    elif city=='Tampa':
        return 220
    elif city=='Pittsburgh':
        return 222
    elif city=='Los Angeles':
        return 475
def rental_car_cost(days):
    
    cost =40*days
    if days>=7:
        cost =cost- 50
    elif days>=3:
        cost =cost- 20
    return cost
def trip_cost(days,city):
    return plane_ride_cost(city)+rental_car_cost(days)+hotel_cost(days)

#17

Here this will fix it :slight_smile:,


#18

Hi

I am getting the error "TypeError: cannot concatenate 'str' and 'int' objects"

Here is the code

def hotel_cost(nights):
return 140 * nights
def plane_ride_cost(city):
if city == "Charlotte":
return 183
elif city == "Tampa":
return 220
elif city == "Pittsburgh":
return 222
elif city == "Los Angeles":
return 475
else:
return 'We don\'t provide service for this Destination'
def rental_car_cost(days):
rent = 40 * days
if days >= 7:
rent = 40 * days - 50
return rent
elif days >=3 and days <=7:
rent = 40 * days - 20
return rent
else:
return rent
def trip_cost(city,days):
return plane_ride_cost(city) + hotel_cost(days) + + rental_car_cost(days)

the plane_ride_cost() function is returning the string not integer
how to rectify it.


#19

Call your function with one of the cities as a parameter and it should return an integer. :slight_smile: And the error is here,


#20

It solved my problem too. But, why does it behave like that?


#21

like what the calling of the function or parameters?


#22

Thanks to this thread I finally got past this section of the course. I'm still unsure in my understanding on how functions pass data in terms of the number of arguments/type and the variables used in the function and their order. Is there a more indepth lesson on this or an external site that provides more information with examples on this? Thanks