python 2.7 - maximum recursion depth error? -
so i'm new (3 days) , i'm on code academy, i've written code 1 of activities when run it displays maximum recursion depth error, i'm running in python console of code academy , simultaneously on own ipython console. hint on page not helpful, can explain how fix this?
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)
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 best, 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)
Comments
Post a Comment