[Tutor] How to make comparison work

Alan Gauld alan.gauld at btinternet.com
Fri Apr 11 10:20:15 CEST 2014


On 11/04/14 00:27, Gregg Martinson wrote:
> I have been working through a fairly simple process to teach myself
> python and I am running into a problem with a comparison.  Can anyone
> tell me where I am going wrong?
>
> #!/usr/bin/env python
>
> class Team(object):
>      code = ""
>      opponents_debated=[]
>      wins=0
>      losses=0
>      competitors=[]
>

All of these variables are class variables rather than instance 
variables. That means they are *shared* by all instances.

>      def __init__(self, code):
>          self.code = code
>          self.competitors.append(code)
>          #self.school_teams.append(code)

So when you run init() you are changing the values for all your teams.
I strongly suspect you want those variables inside init so that each 
object has its own value?


>      ####HERE'S THE LOGIC PROBLEM
>      def havedebated(self, otherTeam):
>          print (self.code, "compares ", otherTeam, "is in",self.competitors)
>          if otherTeam in self.competitors:
>              return 1
>          else:
>              return 0

When you do the comparison you are checking against the shared 
collection which will, I think, have all the teams in it, so it will 
always be true.

I haven't studied that in detail but that's what a quick
glance suggests to me.

hth
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list