2016-07-25 55 views
-2

我在代码学院的练习中为假期费用制定了一个模型,并且我有至今定义的三个函数rental_car_costs,参数days,hotel_cost,参数nightsplane_ride_cost的参数为city。代码如下所示:超过最大递归深度:Code Academy;休假5/7

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

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

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

所有的作品,我都没有问题,但我想创建一个名为trip_cost功能,我不断收到超过最大递归深度。该代码看起来像这样

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

我晚上的值传递给天,以防万一我试着反正代的夜晚,但我仍然得到完全相同的错误消息。我做错了什么,最大深度递归超过了什么意思?

+0

欢迎使用stackoverflow。你的问题缺乏细节。阅读此:http://stackoverflow.com/help/how-to-ask – Shubham

+0

我拿走了“返回hotel_cost(天)”,这是所谓的问题,但现在它说“plane_ride_cost('夏洛特')提出了一个错误:超过最大递归深度“我试图带走”返回plane_ride_cost(城市),并将其放在函数的其余部分之下,但是当我这样做时,它说坦帕返回183,而不是它的正确值220. – Nertfertsatwork

回答

0

这应该工作:

def hotel_cost(nights): 
    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): 
    rental_car_cost = 40 * days 
    if days >= 7: 
     rental_car_cost -= 50 
    elif days >= 3: 
     rental_car_cost -= 20 
    return rental_car_cost 
+0

我一开始并没有看到这个,从我认为没有答案的地方得到了小便。非常感谢 – Nertfertsatwork

+0

现在我得到这个错误“trip_cost('Los Angeles',8)引发了一个错误:不受支持的操作数输入 - =:'str'和'int'“,并且所有内容与您输入的内容完全相同,并且我的旅行代码代码如下所示:def trip_cost(days,city): return hotel_cost(days )+ rental_car_cost(city)+ plane_ride_cost(city) return trip_cost 我试过它作为return trip_cost并返回trip_cost(天,夜) – Nertfertsatwork

0
def hotel_cost(nights): 
     return 140 * nights 
    def plane_ride_cost(city): 
     if city == ("Charlotte"): 
      return 183 
     elif city == ("Tampa"): 
      return 220 
     elif city == ("Pittsburgh"): 
      return 222 
     else: 
      return 475 
    def rental_car_cost(days): 
     if days >= 7: 
      return (40 * days) - 50 
     elif days >= 3: 
      return (40 * days) - 20 
     elif days < 3: 
      return (40 * days) 
     else: 
      return 0  
    def trip_cost(city, days): 
     return (hotel_cost(days) + plane_ride_cost(city) +   rental_car_cost(days)) 
     return trip_cost 

这个答案的工作。