Python 返回人数增加
我是python新手,我的代码有问题。我的函数count\u transitive\u sellers\u with_degree仅从person sellers返回count。我需要得到它返回计数的人卖家和卖家卖家等。我不知道怎么写。。你能帮我说说哪里有问题吗?谢谢:) 它应该写什么…->一,Python 返回人数增加,python,Python,我是python新手,我的代码有问题。我的函数count\u transitive\u sellers\u with_degree仅从person sellers返回count。我需要得到它返回计数的人卖家和卖家卖家等。我不知道怎么写。。你能帮我说说哪里有问题吗?谢谢:) 它应该写什么…->一, 它正在写什么…->0您的问题就在这里。如果条件不允许您执行递归部分,因为Tom的学位不是“bc” if seller.degree == degree: count += 1 +
它正在写什么…->0您的问题就在这里。如果条件不允许您执行递归部分,因为Tom的学位不是“bc”
if seller.degree == degree:
count += 1 + count_transitive_sellers_with_degree(seller,degree)
不反映你说你的程序应该做什么:它只会添加拥有相同学位的人的子女目前你只会给拥有你所测试学位的卖家打电话,但我认为这应该有效
def count_transitive_sellers_with_degree(person, degree):
count = 0
for seller in person.sellers:
if seller.degree == degree:
count += 1
# A seller's sellers should be counted irrespective of
# if they have the degree or not
count += count_transitive_sellers_with_degree(seller,degree)
return count
当您使用调试器时,在什么情况下,行为与您预期的不同?我尝试使用tom=Person('tom',1993,'bc'),它是正确的。。所以我意识到这不包括其他卖家……我应该在if条件中添加更多的东西吗?将递归部分从if条件中带出来
if seller.degree == degree:
def count_transitive_sellers_with_degree(person, degree):
count = 0
for seller in person.sellers:
if seller.degree == degree:
count += 1
# A seller's sellers should be counted irrespective of
# if they have the degree or not
count += count_transitive_sellers_with_degree(seller,degree)
return count