So, I have two instances of a class Person and I am trying to do some comparisons. sun
is one of several attributes of each instance is defined by two randomly generated numbers. I have the following questions... Is creating a subclass like Match
the right way to go about setting up this comparison? And I want an overall
"score" compiled at the end of this comparison, can I do that without the for-loop?
Person1 = Person("Person1")
Person2 = Person("Person2")
class Match(Person):
overall = 0
def __init__(self, Person1, Person2):
self.Person1 = Person1
self.Person2 = Person2
def suns (self):
if abs(Person1.sun - Person2.sun) == 2 or 4 or 8 or 10:
overall += 4
elif abs(Person1.sun - Person2.sun) == 3 or 9:
overall -= 6
elif abs(Person1.sun - Person2.sun) == 6:
overall += 3
else:
overall += 0
print Match.overall
EDIT To clarify what I am trying to do... I have some basic understanding of how the astrological birth chart compatibility services work and just for fun and practice I want to make a very basic version of that program run. For now, all the data is randomly generated and no user input is needed. Here is what the Person class looks like
from random import randint
from decimal import Decimal
def randomDecimal(a,b):
return (randint(int(a), int(100.0 * b)))/100.0
class Person:
def __init__(self, name):
self.name=name
self.sun = (randomDecimal(1, 12), randomDecimal(0, 30))
self.moon = (randomDecimal(1, 12), randomDecimal(0, 30))
self.mercury = (randomDecimal(1, 12), randomDecimal(0, 30))
self.venus = (randomDecimal(1, 12), randomDecimal(0, 30))
self.mars = (randomDecimal(1, 12), randomDecimal(0, 30))
def printer(self):
print "your Sun is in %f, at %f degrees" % (self.sun[0], self.sun[1])
print "your Moon is in %f, at %f degrees" % (self.moon[0], self.moon[1])
#and so on and so forth for the printer
In Python, this line
actually means
Any non-empty value is regarded as True. so this expression will be always True. The proper form
The same goes fp the second if too
If you are trying to compare two people, the comparison itself isn't also a person.
Python classes have special methods you can overide that allow inbuilt python equalities to work.
For example:
These will then allow you to do things like:
Obviouusly, how to compare people is difficult and application specific, but once the logic is in it makes sence.
For your work, where you need to compare to people in the abstract "are they a good fit" sence, `cmp won't work as thats just is one thing greater than, less than or equal to another.
In those cases, you can do the following
Then:
Edit: Making it more specific for you (although the principle is the same):