我是python的新手,我的代码有问题。我的功能count_transitive_sellers_with_degree仅从个人卖家返回计数。我需要得到它的人卖方和卖方卖方的退货计数等。我不知道该怎么写。您能帮我说出问题所在吗?谢谢 :)
class Person:
def __init__(self, name, year_of_birth, degree):
self.name = name
self.year_of_birth = year_of_birth
self.degree = degree
self.leader = None
self.sellers = []
def create_leadership(leader, seller):
seller.leader = leader
leader.sellers.append(seller)
def count_transitive_sellers_with_degree(person, degree):
count = 0
for seller in person.sellers:
if seller.degree == degree:
count += 1 + count_transitive_sellers_with_degree(seller,degree)
return count
>>> martin = Person('Martin', 1991, 'phd')
>>> tom = Person('Tom', 1993, 'mgr')
>>> josh = Person('Josh', 1995, 'bc')
>>> create_leadership(martin, tom)
>>> create_leadership(tom, josh)
>>> count_transitive_sellers_with_degree(martin, 'bc')
它应该写什么...-> 1
现在在写什么...-> 0
最佳答案
目前,您只对本身拥有您要测试的学位的卖家打电话给count_transitive_sellers_with_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
关于python - 返回人数更多,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47584535/