奇怪的循环

时间:2017-12-03 15:48:24

标签: python

我的for循环表现得很奇怪。我想得到一个团队成员的名字,但我没有得到一个姓氏三。你能告诉我哪里可能出问题吗?

class Animal:
    def __init__(self, name):
        self.name = name

class Team:
    def __init__(self, name):
        self.name = name
        self.members = []        

    def add_member(self, member):
        self.member = member
        self.members.append(team.member)

def print_team(team):
    list_members= []
    for member in team.members:
        list_members.append(team.member.name)

    print (list_members)

team = Team('Wolves')
team.add_member(Animal('Josh'))
team.add_member(Animal('Janette'))
team.add_member(Animal('Peter'))
print_team(team)

我正在 - >彼得,彼得,彼得

我想得到 - >乔希,珍妮特,彼得

3 个答案:

答案 0 :(得分:2)

为了便于阅读,你应该总是尝试将与类相关的东西(如打印函数)放在类本身中。那么这样做:

class Animal:
    def __init__(self, name):
        self.name = name

class Team:
    def __init__(self, name):
        self.name = name
        self.members = []        

    def add_member(self, member):
        self.members.append(member)

    def print_team(self):
        print([i.name for i in team.members])

    def __str__(self):
        return str([i.name for i in team.members])

def print_team():
    print([i.name for i in team.members])

team = Team('Wolves')
team.add_member(Animal('Josh'))
team.add_member(Animal('Janette'))
team.add_member(Animal('Peter'))

print_team() # option 1 - outside - not recommended
team.print_team() # option 2 - inside
print(team) # option 3 - inside using __str__ (best I'd say)

打印3次:

['Josh', 'Janette', 'Peter']

答案 1 :(得分:1)

add_member中行.member的目的是什么?这会将对象的属性.last_added设置为最后添加的成员。使用def add_member(self, member): self.members.append(member)之类的名称 或者使用list_members.append(team.member.name) ,直接附加调用函数时传递的对象。

但是,这对我来说并不重要。在循环中,您需要将list_members.append(member.name)更改为def print_team(team): list_private_to_func = [] for object in team.members: list_private_to_func.append(object.name) print(list_private_to_func) 。您正在添加最后添加的成员,如上所述。当你改变它时,你将添加你循环的成员。这将使差异更加显着:

Fatal error: Uncaught Error: Class 'wpdb' not found in /customers/6/a/c/superhuggable.com/httpd.www/wp-includes/load.php:404 Stack trace: #0 /customers/6/a/c/superhuggable.com/httpd.www/wp-settings.php(106): require_wp_db() #1 /customers/6/a/c/superhuggable.com/httpd.www/wp-config.php(98): require_once('/customers/6/a/...') #2 /customers/6/a/c/superhuggable.com/httpd.www/wp-load.php(37): require_once('/customers/6/a/...') #3 /customers/6/a/c/superhuggable.com/httpd.www/wp-blog-header.php(13): require_once('/customers/6/a/...') #4 /customers/6/a/c/superhuggable.com/httpd.www/index.php(17): require('/customers/6/a/...') #5 {main} thrown in /customers/6/a/c/superhuggable.com/httpd.www/wp-includes/load.php on line 404

是的,@ Anton vBR可以更好地解决这个问题!

答案 2 :(得分:0)

您在print_team中使用team.member.name。它始终需要team.member的最后初始化值。

您还可以对功能进行排序, print_team()中不需要使用循环。 只需在add_memeber函数中保存member.name。

    class Animal:
        def __init__(self, name):
            self.name = name

    class Team:
        def __init__(self, name):
            self.name = name
            self.members = []        

        def add_member(self, member):
            self.member = member
            self.members.append(member.name)

    def print_team(team):
        print team.members


    team = Team('Wolves')
    team.add_member(Animal('Josh'))
    team.add_member(Animal('Janette'))
    team.add_member(Animal('Peter'))
    print_team(team)