[Tutor] Why does the last loop not work?

Gregg Martinson gregg.martinson at gmail.com
Tue Feb 25 01:59:24 CET 2014


I am trying to generate a list of teams using objects that I collect into a
list that I can cycle through.  But when I run the last loop there is
nothing produced.  I am pretty sure it has to do with my syntax for the
list and the substitution of a a local variable but I can't figure out how
to make it work.  Any help would be appreciated.  I am working my way
though *Learning Python*, but its a long slog.

#!/usr/bin/env python
def printit (aff,neg):
    print (aff, "\t",neg,"\tTBD\tTBD")

def header (r):
    print ("##############################")
    print ("### Round: ",r,"            ####")
    print ("##############################")
    print ("Aff\tNeg\tJudge\tRoom")




class Team(object):
    code = ""




    # The class "constructor" - It's actually an initializer
    def __init__(self,code):
        self.code = code
        print ("code is: ",code)
        self.competitors=[]
    def print_team(self):
        print("team code is: ",self.code)
        print("debated:",end=" ")
        for x in self.competitors:
            print (x)

    def debated(self,otherTeam):
        print (x)
        self.competitors.append(x)
    def giveCode(self):
        return self.code


def make_team(code):
    team = Team(code)
    return team

#MAIN Program#
myTeamCodes=["a","aa","b","bb","c","cc","d"]
# Make teams
myTeams=[]#list of teams
for x in myTeamCodes:
    myteam=make_team(x)
    myTeams.append(myteam)
# problem is that myTeams has no value outside of the loop
for x in myTeams:
    x.print_team
--
http://about.me/greggmartinson
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20140224/11b60af8/attachment.html>


More information about the Tutor mailing list