我有两个类,其中的对象分别包含关于个人和家庭的信息。person类中有一个函数,如果给定的人是后代,则该函数应返回true。我可以找到答案,但是我不知道在进行了几个嵌套函数调用之后如何返回它的值
这是我的两个函数,在不同的类中
函数,该函数最初被调用以查看子体是否存在。如果存在,我希望它返回true
def checkIfDesc(self, person):
for family in self.marriages:
return families[family].famDescendant(person)
来自另一个类的函数,该类保存有关族的信息。i、 给孩子们打电话
def famDescendant(self, person):
for child in self.children:
if person == child:
return True
else:
return persons[child].checkIfDesc(person)
现在,我认为我的问题在于checkIfDesc函数及其返回语句。如果在第一次迭代中未找到子体,它将停止搜索。如果我删除return语句,基本上只是遍历从输出中可以看到的族,那么我输入person==child语句
任何帮助或提示都将不胜感激
此函数
正如您所发现的,不会执行您想要的操作,因为如果
self
有多个婚姻,并且person
不是第一个婚姻的后代,那么函数将返回False
,而不检查其他婚姻。您可以这样做:一旦找到后代,将停止检查
通过实施这些变更来解决:
相关问题 更多 >
编程相关推荐