Python Forum
Newbie here - how do i compare\check 2 attributes of an instance
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Newbie here - how do i compare\check 2 attributes of an instance
#1
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()
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to add class instance attributes from list 999masks 2 2,730 Jul-22-2019, 07:59 AM
Last Post: 999masks
  How do I sum instance attributes? pythonprogrammer 9 7,094 Jul-18-2019, 07:13 AM
Last Post: perfringo
  Class object instance. Link instance attribute to class. Can it be done easier. Windspar 7 4,168 Dec-03-2018, 11:16 PM
Last Post: Windspar
  How to check if class instance exists in a list of class instance objects? sonicblind 23 20,335 May-27-2018, 05:44 AM
Last Post: buran
  Python3x running only with one instance (how can I prevent too many running instance) harun2525 5 18,265 Jul-21-2017, 07:36 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020