1

So I'm really new to this (3 days) and I'm on code academy, I've written this code for one of the activities but when I run it it displays maximum recursion depth error, I'm running it in the python console of code academy and simultaneously on my own ipython console. The hint on the page is not helpful, can anybody explain how to fix this? Thanks

def hotel_cost(nights):
    return (nights * 140)

def plane_ride_cost(city):
    if plane_ride_cost("Charlotte"):
        return (183)
    if plane_ride_cost("Tampa"):
        return (220)
    if plane_ride_cost("Pittsburgh"):
        return (222)
    if plane_ride_cost("Loas 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(nights) + plane_ride_cost(city) + rental_car_cost(days)
1
  • edit I am aware of the spelling mistake :') Commented Jul 28, 2016 at 8:38

1 Answer 1

1

Maybe:

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)

The error was:

The plane_ride_cost(city) called plane_ride_cost("Charlotte") in every recursion step.

Not the best, but a better approach:

def hotel_cost(nights):
    return nights * 140

plane_cost = {
    'Charlotte' : 183,
    'Tampa' : 220,
    'Pittsburgh' : 222,
    'Los Angeles' : 475,
}

def plane_ride_cost(city):
    if city not in plane_cost:
        raise Exception('City "%s" not registered.' % city)
    else:
        return plane_cost[city]

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(nights) + plane_ride_cost(city) + rental_car_cost(days)
Sign up to request clarification or add additional context in comments.

1 Comment

better use a dict, raise an Exception if city not in the dict.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.