Taking Vacation 6/7 lesson problem with returning the correct value


#1
def hotel_cost(nights):
  # the hotel costs 140  a night
  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 -= 50 
  elif days >= 3: 
      cost -= 20 
  return cost 

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

HERE COMES THE PROBLEM:

trip_cost(‘Los Angeles’, 3, 0) returned 855 instead of the correct value 995

However, all the values are the ones that I was asked to put in, thus my bewilderment. But then again, I am an extreme rookie with programming so perhaps I can’t see the obvious. Help much appreciated! :slight_smile:


#2

the difference between the correct and incorrect total is 140, which is the cost of a night. I am afraid that this program needs the same amount as days and night when going on a holiday


#3

Cheers for bringing my attention to it. It worked!
All the best!


#4

So it seems to me like the exercise needs to be corrected. In the lesson 5/7, it is explained that we converted the variable [night] into [days - 1] for the [hotel_cost] function so that we only have to deal with 2 parameters for the function [trip_cost]: days and city. This makes sense as you won’t be staying at the hotel on the day in which you’re leaving the vacation destination.

But the issue arises when we move to lesson 6/7, which doesn’t account for this [days - 1 ] conversion for the [nights] variable.

So in lesson 6/7, we’re staying for the same number of nights as days, which is

  1. false, per the explanation from lesson 5/7.
  2. in direct contradiction to what was established in lesson 5/7.

I think the script checker for lesson 6/7 should be corrected so that the values for the hotel_cost portion of the [trip_cost] function should be [days - 1] and not [days] to match the information provided in lesson 5/7.


#5

It didn’t use to be days - 1, but i agree that its a good addition, but then the exercise validation doesn’t seem to have been updated, let me check. I will get back to you.


#6

I have exactly the same problem as described above, solved by changing (days-1) into (days) on the hotel_cost function which now reads as following:

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


#7

To help everyone here. Here’s how you can get past it, since it’s clearly wrong:

#changed (nights) to (days) in this function in both places
def hotel_cost(days):
return 140 * days

#in the trip_cost function, change (days-1) to just (days)
def trip_cost(city, days, spending_money):
tripCost = rental_car_cost(days) + hotel_cost(days) + plane_ride_cost(city) + spending_money
return tripCost


#8

This error still occurs. Took me a while to figure out what was going wrong!


#9

Yeah, you end up working it all out in a notepad or spreadsheet document to see what the hidden calculation might be. It would be good if you could get more than just the input and expected output for the test data.


#10

who says you can’t? Adding function calls and print statement to see what your code is doing is actually really good :slight_smile:


#11

Oh right! So, I should put in all the print statements I want, and then run CA’s test code. Good idea, thanks!


#12

yes, but for function you might need to also add a function call (given functions only execute when called), don’t rely on codecademy test cases to give you the output


#13

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