#5 Pull it Together / I give up: the solution, please!


#1

I give up. Here's what happens to be the last of my attempts at the code:

def hotel_cost(nights):
return 140.0 * 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 -= 50
elif days >= 3:
cost -= 20
return cost

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

print trip_cost("Pittsburgh", 10)


#2

As I have stated before in this thread multiple times, this specific section is terrible for beginners.

Here is the solution I like

city_prices = {
    "Charlotte": 183,
    "Pittsburgh": 222,
    "Tampa": 220,
    "Los Angeles": 475
}

def hotel_cost(nights):
    return 140 * nights

def plane_ride_cost(city):
    return city_prices[city]

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

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

#3

Thank you for your reply zeziba.
However, the code you provide get this error at my end:
"Oops, try again. trip_cost should take exactly 2 arguments!"

And in Pythonfiddle it gets this error:
File "", line 22
print return hotel_cost(days) + rental_car_cost(days) + plane_ride_cost(city) + spending_money
^
SyntaxError: invalid syntax



#4

You are one behind the spending cost part, just take that out and it should work.

Double check though.


#5

Thank you, but I don't know what you mean by "one behind the spending part".


#6

Is there really no place in Codecademy to go for a solution?


#7

Well, this is what I did:

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):
    total = days * 40
    if(days >= 7):
        total -= 50
    elif(days >= 3):
        total -= 20
    return total
def trip_cost(city, days):
    return rental_car_cost(days)+hotel_cost(days)+plane_ride_cost(city)

#8

Remember in python "Flat is better than nested".

import this

That is why I got rid of the if/else statements from the plane_ride_cost

And what you have for trip-cost confirms what I though about the spending cost, that comes in next.


#9

Hi ragezapper,

And did that work for you? Here it says:
Oops, try again. trip_cost should take exactly 3 arguments!


#10

It works for me. :neutral_face:


#11

If your rental_car_cost is set up different it won't work I had mine set up like this

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

So the solution posted didn't work, after changing the way I set up rental_car_cost ragezappers solution worked.


#12

I'm having similar issues. Where is my code wrong/incorrect?

city_prices = {
"Charlotte": 183,
"Pittsburgh": 222,
"Tampa": 220,
"Los Angeles": 475
}

def hotel_cost(nights):
return 140 * nights

def plane_ride_cost(city):
return city_prices[city]

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):
    return rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city)

#13

Thanks again, ragezapper!
I'm back at it, but still don't have anything that works.

zeziba's solution is for the NEXT exercise.
I'm stuck on #5, not #6.


#14

To answer my own question at the outset of this thread:

In this:
°
°
°
def trip_cost(city, days):
return hotel_cost(nights) + plane_ride_cost(city) + rental_car_cost(days)

My mistake was nights as the argument in hotel_cost. It needs to be days, the same as for rental_car_cost, like so:
def trip_cost(city, days):
return hotel_cost(days) + plane_ride_cost(city) + rental_car_cost(days)


#15

my mistake was the line "def trip_cost(city, days):" wasn't indented. while that took away the error "define a trip_cost", I'm now getting an error about pittsburg and maximum recursion being reached.


#16

Hi dataninja03133, completed, mine looks like this:

def hotel_cost(nights):
    return 140.0 * 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 = days * 40
    if days >= 7:
       cost -= 50
    elif days >= 3:
       cost -= 20
    return cost    
    
def trip_cost(city, days):
    return hotel_cost(days) + plane_ride_cost(city) + rental_car_cost(days)

#17

Did the same thing and it works

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


#18

what the ■■■■ where are the answers


#19

THX SO MUCH! I WAS SO STUCK! :slightly_smiling::slightly_smiling:


#20

the whole entire function is using nights so how can i just change it to days???