[Tutor] How to make comparison work
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 = ""
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
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
> 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.
Author of the Learn to Program web site
More information about the Tutor