Reputation: 9
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)
Upvotes: 1
Views: 55
Reputation: 5783
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)
Upvotes: 1