FAQ: Taking a Vacation - Pull it Together

This community-built FAQ covers the "Pull it Together " exercise from the lesson “Taking a Vacation”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Python

FAQs on the exercise _Pull it Together _

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi all,

I’m about to do this exercise but I just have a couple of issues with the example: Why are the parameters (arguments) different for the third to the first and second? And what does 'returns the sum of the previous two functions when called with a and b, respectively.

Many thanks
Josh

3 Likes

Whenever I press run, an error message pops up saying “trip_cost(‘Charlotte’, 3) returned ‘563’ instead of the correct value 563” or different values. It always says it returns x instead of x, and even though it’s always correct, it never says it is. What did I do wrong?

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

There is no spending_money value in your code.

This worked for me:

#hotel cost function
def hotel_cost(nights):
  #cost is fixed at $140
  return 140 * nights

#plane cost function
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 "There are no flights from that city."

#rental car cost funciton
def rental_car_cost(days):
  cost = 40 * days
  if days >= 7:
    cost += - 50
  elif (days >= 3) and (days < 7):
    cost += - 20
  return cost

#put it all together
def trip_cost(city, days):
  return hotel_cost(days - 1) + plane_ride_cost(city) + rental_car_cost(days)

This is also what I am wondering.

def hotel_cost(night) :
return 140 * night

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

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

So that when you put the amount of days as a parameter, it will automatically plug it into both the rental_car_cost and hotel_cost functions. So there are two parameters and three functions because one of the parameters (days) works for two of the functions.

Majority of code removed. Checker appears to assume no error checking and rejects code as a result. Reference the delta between hotel_cost and hotel_cost_bad functions and with 0 days the trip_cost would return 80 vs 220. Hotel pays you for not staying there.

print “Start”
def hotel_cost(nights):
if (type(nights) == int) or (type(nights) == float):
if int(nights) >=0 :
return 140*int(nights)
else: #int(nights) <= 0:
return 0
else:
print “arg needs to be int or float”
return False

def hotel_cost_bad(nights):
if (type(nights) == int) or (type(nights) == float):
if int(nights) >=0 :
return 140int(nights)
else: #int(nights) <= 0:
print “Allow hotel to pay you for not staying there!”
return 140
int(nights) #0
else:
print “arg needs to be int or float”
return False

print “test hotel”
print hotel_cost(1)
print hotel_cost(0)

def plane_ride_cost(city):

print (type(city) == str)

print city.lower()


elif city.lower() == “tampa”:
return 220

#print “debug”
#plane_ride_cost(“Charlotte”)

def rental_car_cost (days):

print “cost_analysis definition function same function as trip_cost, but cost analysis function has some error checking”
def cost_analysis (city, days):
trip_sum = rental_car_cost(days)
trip_sum += hotel_cost(days -1)
trip_sum += plane_ride_cost(city)
print “trip_sum %s to %s for %s days” %(trip_sum, city, days)
return trip_sum

def trip_cost (city, days):
trip_sum = rental_car_cost(days)
trip_sum += hotel_cost_bad(days -1)
trip_sum += plane_ride_cost(city)
print “trip_sum %s to %s for %s days” %(trip_sum, city, days)
return trip_sum

print “Sample error checking start…”
print “…Example 1…”
print “cost_analysis function call”
sum1 = cost_analysis(“los angeles”, 5.6)
print “trip_cost function call”
sum2 = trip_cost(“los angeles”, 5.6)
evaluation = sum1 == sum2
print “Result funtions return same value %s” %(evaluation)
print “…Example 2…”
sum1 = cost_analysis(“tampa”, 0)
sum2 = trip_cost(“tampa”, 0)
evaluation = sum1 == sum2
print “Result funtions return same value %s” %(evaluation)
print “Sample error checking end…”

Hi - I got 2 questions:

  1. Why do we need to specify parameters for the function of adding the total of all three costs if these parameters are already included in each cost category - isn’t this complicating matters beyond what is needed? A simple addition of three totals shouldn’t need further parameters, no?

  2. How does Python know we are referring to days-1 if the initial function was defined with “nights” as the parameter? Nowhere was this change put into the system?

Thanks for your help

literally same. i checked my code against the example & answer and i see zero differences

1 Like

I’ve had the same issues on a number of exercises. But if I hit “View Solution”, erase their code, then paste mine, it accepts my code.

1 Like

Same here. But when I put my code, it said define trip_cost when I had already done so. THE CODE IN THE SOLUTION WAS LITERALLY THE SAME!

1 Like

My code keeps running and running with no end on sight.
I can’t find any errors.

def hotel_cost (days-1):
return 140 * (days-1)

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(days-1) + plane_ride_cost(city) + rental_car_cost(days)

print trip_cost(“Tampa”, 2)

I am facing a problem in my code everytime i give input and it doesnt gives any output any one help me to understand where is the problem in my code.

My Codes are -

Hotel Cost Per nights :

def hotel_cost(days):

return 140 * days

Flight cost for different cities:

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

Per Day Car Ride cosy:

def rental_car_cost(days):

per_day_cost = 40 * days

if days >= 7:

per_day_cost = per_day_cost - 50

return per_day_cost

elif days >=3:

per_day_cost = per_day_cost - 20

return per_day_cost

else:

return per_day_cost

#Total trip Cost:

def trip_cost(city,days):

total = rental_car_cost(days)+hotel_cost(days - 1)+ plane_ride_cost(city)

Have a look at this FAQ concerning code formatting which helps immensely when posting queries-

What is the exact issue? Does it throw an error or does the code execute? What output were you expecting from your given input? Is there code to perform this?

This message pop “trip_cost(‘Tampa’, 4) returned None instead of the correct value 780”. I don’t understand where i am getting wrong in functions my codes run but i did’nt get any output.

Here’s my code:
“”"

Hotel Cost Per nights :

def hotel_cost(days):

return 140 * days

Flight cost for different cities:

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

Per Day Car Ride cosy:

def rental_car_cost(days):

per_day_cost = 40 * days

if days >= 7:

per_day_cost = per_day_cost - 50

return per_day_cost

elif days >=3:

per_day_cost = per_day_cost - 20

return per_day_cost

else:

return per_day_cost

#Total trip Cost:

def trip_cost(city,days):

total = rental_car_cost(days)+hotel_cost(days - 1)+ plane_ride_cost(city)
“”"

It’s a backtick/backquote character you need to wrap chnks of code with-
` ` `
code goes here
` ` `

If it’s a message that you didn’t code then I’d assume it’s on of the background tests that will be run on your code in certain lessons to make sure it meets the requirements.

Breaking down that message the function trip_cost was called with two arguments ('Tampa', 4) and the value returned was None rather than the expected value 780. Since you don’t actually use a None object anywhere it must have come from somewhere else.

The most likely candidate is that if you have a function that doesn’t actually return anything it defaults to returning None. Could this have occurred in your code?

So I tried the same and got a syntax error for def hotel_cost(days - 1):

I think we are supposed to leave the argument as is def hotel_cost(nights)
but then when return the sum we call it as def hotel_cost(days - 1) .

In the example they showed, that you names of the parameters can be different.

Cheers
Andy

The syntax error is because you can’t use expressions for parameter names. It’s basically the same as using x - 3 = 'fifteen', it’s trying to assign to a name x - 3 which is not a valid parameter name. You can pass expressions as arguments to a function though (e.g. print(10 - 3)), they simply get evaluated before they are passed. It’s the bit about creating a new name or reference with invalid syntax that throws an error.

1 Like