Recursion 超过最大递归深度:代码学院;7月5日休假
我在一个代码学院的练习中为假期成本做了一个模型,到目前为止,我定义了三个函数,Recursion 超过最大递归深度:代码学院;7月5日休假,recursion,max,depth,Recursion,Max,Depth,我在一个代码学院的练习中为假期成本做了一个模型,到目前为止,我定义了三个函数,租车成本,参数为天,酒店成本参数为夜,参数为城市。代码如下所示: def hotel_cost(nights): return hotel_cost(nights) return 140 * nights def plane_ride_cost(city): return plane_ride_cost(city) if "Charlot
租车成本
,参数为天
,酒店成本
参数为夜
,参数为城市
。代码如下所示:
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\u cost
的函数,并且我不断获得超过最大递归深度的值。代码如下所示
def trip_cost(city, days):
return plane_ride_cost(city) + hotel_costs(days) + rental_car_cost(days)
我将夜晚的值传递给白天,以防万一我尝试在中替换夜晚,但仍然会收到完全相同的错误消息。我做错了什么?超过最大深度递归意味着什么?这应该可以:
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
def酒店成本(夜):
返回140*晚
def飞机驾驶费用(城市):
如果城市==(“夏洛特”):
返回183
埃利夫市==(“坦帕”):
返回220
埃利夫市==(“匹兹堡”):
返回222
其他:
返回475
def租车费用(天):
如果天数>=7:
返回(40*天)-50
elif天数>=3天:
返回(40*天)-20
elif天数<3天:
报税表(40*天)
其他:
返回0
def差旅费用(城市,天数):
返回(酒店费用(天)+飞机费用(城市)+租车费用(天))
回程费用
这个答案有效。欢迎来到stackoverflow。你的问题缺乏细节。阅读此文:我删除了“返回酒店费用(天)”,这是所述的问题,但现在它说“飞机驾驶费用('Charlotte')引发了一个错误:超过了最大递归深度”。我尝试删除“返回飞机驾驶费用(城市)”,并将其放在函数其余部分的下面,但当我这样做时,它会说tampa返回183,而不是它的正确值220。我起初没有看到这一点,并对我认为缺少答案感到恼火。非常感谢,我现在收到这个错误“trip_cost('Los Angeles',8)引发了一个错误:不支持的操作数类型对于-=:'str'和'int'“一切都和你输入的一模一样,我的旅行费用代码看起来像这样-def trip_cost(天,城市):return hotel_cost(天)+rent_car_cost(城市)+plane_ride_cost(城市)return trip_cost(天,夜),我试过它作为回程费和回程费(天,夜)
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