Pull it together: I don't know what's wrong


#1

Below is my whole exercise, no matter what I try I keep getting the error message- that the cost for given city was calculated incorrectly.

Many thanks in advance!


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
    else:
        return cost
def trip_cost(city, days ):
    days = 3
    return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost("Tampa")


#2

In your last function, you are setting the city to "Tampa" every time. You want to use the 'city' parameter that you created for function with so you can call any city. The same goes for the number so you can use any number of days. So why are you setting days in your function? That defeats the purpose of creating the parameter. You want to be able to say:

trip_cost("Tampa", 3)

So the function should be:

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

#3

I changed it to a specific city after reading one of the responses to a similar question. Now I changed the last function back to 'city' and I keep getting the same error message:
Oops, try again. trip_cost('Tampa', 9) returned 740 instead of the correct value 1790


#4

You are also returning the cost incorrectly in your rental_car_cost function. You have a return statement in the wrong place and returns where there shouldn't be. The function should read:

def rental_car_cost(days):
    cost = days * 40
    if days >= 7:
        cost = cost - 50
    elif days >= 3:
        cost = cost - 20
    return cost

That way, it returns the cost AFTER it's calculated the cost.


#5

Yay! Thank you so much @tekkie1618


#6

Or no, wait wait wait, shouldn't there be an 'else' statement under the last line of the part you quoted?
something like

else:
return cost

?

PS I think I fixed the car rental part (the way @tekkie1618 did it, without the 'else' part) (how come it did come through when I was doing the separate car rental cost exercise if it was wrong...?) but again the same error message pops up, the one saying Oops, try again. trip_cost('Tampa', 9) returned 740 instead of the correct value 1790.

Below all I've written, again:

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:
cost = cost - 50
elif days >= 3:
cost = cost - 20
return cost

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


#7

There does not need to be an else statement because you don't want to change the value of cost if it is less than 3. So you could add an else, but there is no reason. It checks to see if the number of days is greater or equal to 7, then if it's not it checks to see if it's greater or equal to 3, then if not it doesn't change the value of cost and continues with the code after the if statement.

You are still setting days to 3 in your function. Take that line out.


#8

Solved it. There was this random 'days=3' (a product of confusion from reading too much on the forum) in the second last line. :nerd:


#10

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