程序有两个函数human_pyramid(no_of_people)
,它们将基数中的no_of_people作为参数,并返回总金字塔的权重
x
x x x
X X X X X <=== this is the argument (no_of_people = 5)
每个人的体重为50,该函数将返回450
def human_pyramid(no_of_people):
if no_of_people == 1:
return 50
else:
return no_of_people*(50)+human_pyramid(no_of_people-2)
第二个函数将max_weight作为参数,并返回不超过最大权重即可构成人形金字塔的人数。
def find_maximum_people(max_weight):
no_of_people=0
while human_tower(no_of_people) <= max_weight:
no_of_people +=1
return no_of_people
max_people=find_maximum_people(1000)
print(max_people)
我在while human_tower(no_of_people) <= max_weight:
行上收到RecursionError,并且
return no_of_people*(50)+human_pyramid(no_of_people-2)
但是单独运行function human_pyramid(no_of_people)
可以正常运行
答案 0 :(得分:1)
if no_of_people == 1:
return 50
else :
return no_of_people*(50)+human_pyramid(no_of_people-2)
仅当最初传递的值是奇数时,条件no_of_people == 1
才会变为True。
作为解决方案,您可以在no_of_people == 0时再添加一个基本案例
if no_of_people == 1:
return 50
elif no_of_people == 0:
return 0
else :
return no_of_people*(50)+human_pyramid(no_of_people-2)