Python Forum

Full Version: Newbie here - how do i compare\check 2 attributes of an instance
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hey there :) got stuck in exercise

made a class of CrazyPlane
now i need to make a infinite loop to get random coordinates for the planes
but if one of the planes is 2 or less coordinates between
i need to make the same plane a turn function to get it away by 1 point
in short i need the planes to not crash :S like a tower control :)
but my problem is i how do i compare between each plane?
do i make a loop?
another function?
any ideas will do
btw my main problem is how do i check if x of american is close to x of every other plane and same for y
if the are close by one than a call for a turn function will come
Thanks
[Image: Ra16HcN.jpg]

import random
 
class CrazyPlane:
 
    def __init__(self,x,y):
        self.__x = x
        self.__y = y
 
    def update_position(self):
        self.__x += random.randint(-1,1)
        self.__y += random.randint(-1,1)
 
    def get_position(self):
        return self.__x,self.__y
 
    def count_down(self):
        for i in range(10,0,-1):
            print i
 
    def __eq__(self, other):
        return self.value == other
 
    def crash_check(planes):
        x = 0
        y = 1
        flag = True
        while (flag):
            if (__eq__(planes[x], planes[y])):
                print " BOOM!"
            else:
                y = y + 1
                if y == 3:
                    x = x + 1
                    y = x + 1
                    if x == 3:
                        flag = false
                        return False
 
 
###AND!!!!!!!!!!!!!!!!!!!####
 
 
 
 
def main():
    elal = CrazyPlane.CrazyPlane(1,2)
    american = CrazyPlane.CrazyPlane(3,4)
    british = CrazyPlane.CrazyPlane(5,6)
    lufthansa  = CrazyPlane.CrazyPlane(7,8)
    planes = [elal,american,british,lufthansa]
    flag = True
 
    while (flag):
        for plane in planes:
            plane.update_position()
            if crash_check(planes):
                flag = False
                print 'Crash'
 
main()
First, your __eq__ is messed up. You are checking value against other, when you don't have a value attribute. I would check a tuple of (self._x, self._y) against a tuple of (other._x, other._y). Also, you don't need to call it and you're calling it wrong. Once it's defined, it overrides the equals operator, so you can just check plane[x] == plane[y]. If you were to call it, you would want to call it as self.__eq__, with just one argument (the plane to check against the current instance).

Second, your crash_check method does not match the diagram. If there is no 'BOOM', you should call an is_close() method, which you have not defined. I'm assuming that when it is done, it would use the Pythagorean theorem to get the distance between the two planes, and check that distance against some definition of 'close.' If it's too close then call the turn() method, again not defined yet. However, given that your planes do not have directions and just move randomly, I'm not sure what turn would do.

Your loop seems a little odd. I would think you would loop through the the planes, move the plane, and check for a crash against each other plane, then check for turn. Then move the next plane, check for crash, check for turn, and so on.
And crash_check needs a self parameter.

And are these two different files? Are you importing CrazyPlane.py into the file with the main function? If not, you just want CrazyPlane(x, y), not CrazyPlane.CrazyPlane(x, y).