Putting it together


#1

please assist i keep getting:
Oops, try again. trip_cost('Los Angeles', 7) returned 1210 instead of the correct value 1685.
not sure what to do or where exactly the error is.

def hotel_cost(nights):
    nights * 140
    return nights * 140
    
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 False
 
def rental_car_cost(days):
    rent = days * 40
    if days >= 7:
        rent = rent - 50
        return rent
    elif days >= 3:
        rent = rent - 20
        return rent
    else:
        return rent


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

#2

there are two problems, both in this lines:

def trip_cost(days,city):
    days = 7

why set days to 7? Days is passed as argument, remove days = 7

When the exercise calls the function, the city is the first argument, and the days the second argument. You swapped the parameters, city should be the first parameter, and days the second paramter


#3

About the thing with setting days to 7, as far as I know, in real-life programming if you define a function and give it a parameter, argument needs to be defined before you define the function. Else, if you try calling it, it will not know what parameter to get and will give you an error, something like argument hasn't been defined anywhere.

Basically it should look like that:

days = 7
def function(days):
    [blah blah]

And basically the line of code I put there says that the function takes a defined argument (days) and only then starts proceeding to do things in next lines. In your example it wouldn't even begin with getting argument, if it was a standalone code.


#4

not true? the parameter will serve as placeholder, until the function is called with an argument, this:

der example(parameter):
   print parameter
example("lets print the parameter")

would be perfectly valid syntax.


#5

Uploading...

Aaah, okay, I think I messed up. I meant that the argument needs to be defined before calling the function. In any case, if you have code like:

def function(days):
    days = 7
    return days

days = 20
function(days)

Am I mistaken in assuming that the called function, even if we put the amount of 20 days in it, will return 7 days? If yes, it's still kind of weird, so still better to define argument before calling the function, outside of code. At least I think so?


#6

yes, that makes sense.

i don't understand why OP did days = 7 inside the function, it is not necessary to overwrite the function parameter, in fact it is part of the reason for the error message.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.