我在代码学院练习中为假期成本制作模型,到目前为止我定义了三个函数,rental_car_costs
带参数days
,hotel_cost
带参数{ {1}}和nights
的参数为plane_ride_cost
。代码如下所示:
city
一切正常,我没有问题,但我想创建一个名为 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
我将夜晚的价值传递给了几天,以防我无论如何都尝试过替换夜晚,但我仍然得到完全相同的错误信息。我做错了什么,超出最大深度递归是什么意思?
答案 0 :(得分: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
答案 1 :(得分: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
这个答案有效。