Part 5 "Pulling It All Together"


#1

This is my code for the section and I'm having a difficult time determining what I'm doing wrong. Every single time it gives me a "maximum recursion depth exceeded". But when I see other examples that worked for users on this site, it seems to look identical.

def hotel_cost(nights):
# hotel costs #140/night. hotel_cost should return 140 nights
hotel_cost(nights)
return 140 * nights

Define a fxn called plane_ride_cost; that takes a string, city.

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

Define a fxn called rental_car_cost; takes an argument in days.

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

Define a fxn called trip_cost; takes 2 arguments: city and days.

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


#2

This right here is your problem.

You created a recursive function with no end, which means that it calls its self into infinity and beyond.

def hotel_cost(days):
    return 140 * days

#3

thank you for the response.

But I copied and pasted your correction.

def hotel_cost(days):
return 140 * days

Now every time I try to "Save & Submit" I get a different error that goes down the list of cities:

  • "trip_cost('Los Angeles', 5) raised an error: unsupported operand type(s) for +: 'NoneType' and 'str'"

  • "trip_cost('Tampa', 2) raised an error: unsupported operand type(s) for +: 'NoneType' and 'str' "

  • " trip_cost('Charlotte', 1) raised an error: unsupported operand type(s) for +: 'NoneType' and 'str'

  • " trip_cost('Pittsburgh', 1) raised an error: unsupported operand type(s) for +: 'NoneType' and 'str'

etc.


#4

Sadly, I believe that this particular section you are on is the worse one in all of the Python course.

below I have posted some code for you to cross examine with your code to find the errors. I have posted about it in multiple threads because it is so poorly written multiple people are having issues with the same stuff. They designed it in such a way that it is extremely error prone for newbies.

Original Post

Here is the code bit for you to examine. I have done away with the if/elif/elif/else garbage that they are trying to force on this course. It is bad form in programming to repeat the same thing over and over again. If you ever see repeating code you know you can reprogram it so you only use that chunk once and call on it again and again.

EXAMPLE:

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):
    car = 40 * days
    if days >= 7:
        car -= 50
    elif days >= 3:
        car -= 20
    return car

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

EDIT
I should also note that the judge for this one is not so good ether and you will have to change the return on the last function to a print!


#5

Thank you so much! Your code is a lot cleaner and is more logical.

I tried your method (by only using the (city,days) arguments) and it works.


#6

And of course that made the next section easier.


#7

Hi Guys,

I have written the code like below,

def hotel_cost(days):
   return 140*days

def plane_ride_cost(city):
    if city == "Charlotte":
        return 183 
    if city == "Tampa":
        return 220 
    if city == "Pittsburgh": 
        return 222
    if city == "Los Angeles":
        return 475

and when I am executing the code I getting following error.

Please help!

File "python", line 4
def plane_ride_cost(city):
^
IndentationError: unindent does not match any outer indentation level

Oops, try again.
Did you create a function called plane_ride_cost?

#8

hi guys,
I had a problem with Pulling it all together but at last I have passed it, so if u want to try my code here it is:

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 "Yo dont't enter the city"

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

def hotel_cost(days):
return 140 * days

def trip_cost (city,days):

return   hotel_cost(days) + plane_ride_cost(city) + rental_car_cost(days) + int()

print trip_cost("Los Angeles", 5, 600)