0

So, I have a Class 'Cube', which has 4 attributes, namely x_coordinate, y_coordinate, z_coordinate and access_point. I have instantiated 1000 objects for the class 'Cube' and have added them to a list named 'Cube_list'. For all the 1000 objects, the attributes 'x_coordinate', 'y_coordinate', 'z_coordinate' have integer values between 0 and 9, while the value for the attribute 'access_point' is set to zero. Next, I ask the user for the coordinates for three access points, 'fx', 'fy', 'fz' for the first access point, 'sx', 'sy', 'sz' for the second access point, and 'tx', 'ty', 'tz' for the third access point. Next, for the three class instances with those three coordinates, we change the attribute 'access_point' to '1', and then append those three instances to another list called 'Access_Point_List'.

My code is running fine except for the last part where I am unable to append those specific objects to the list 'Access_Point_List'.

Here's my complete code:

class Cube:

    def __init__(self, x, y, z, block_access_point):
        self.x_coordinate = x
        self.y_coordinate = y
        self.z_coordinate = z
        self.access_point = block_access_point

    def set_access_point(self, set_ap):
        self.access_point = set_ap


Cube_list = []
Access_Point_List = []

for i in range(10):
    for j in range(10):
        for k in range(10):
            Cube_ijk = Cube(i, j, k, 0)
            Cube_list.append(Cube_ijk)

print("No of elements in cube_list are:", len(Cube_list))
fx, fy, fz = input("Enter coordinates of the first access point: ").split()
sx, sy, sz = input("Enter coordinates of the second access point: ").split()
tx, ty, tz = input("Enter coordinates of the third access point: ").split()

for i in Cube_list:
    if ((i.x_coordinate == fx) and (i.y_coordinate == fy) and (i.z_coordinate == fz)) or     ((i.x_coordinate == sx) and (i.y_coordinate == sy) and (i.z_coordinate == sz)) or ((i.x_coordinate == tx)     and (i.y_coordinate == ty) and (i.z_coordinate == tz)):
        i.set_access_point(1)
        Access_Point_List.append(i)

print("No of elements in access_point_list are:", len(Access_Point_List))

for i in Access_Point_List:
    print(i.x_coordinate, end=" ")
    print(i.y_coordinate, end=" ")
    print(i.z_coordinate)

I checked and confirmed that the 1000 class objects are added correctly to the list 'Cube_list'. I printed the values of 'fx', 'fy', 'fz', 'sx', 'sy', 'sz', 'tx', 'ty', 'tz' and they are accepted and displayed correctly. I verified that the 'i.set_access_point(1)' command is also working as I had expected. But the command 'Access_Point_List.append(i)' is not appending the specific three objects to the list 'Access_Point_List'. The command 'print("No of elements in access_point_list are:", len(Access_Point_List))' is printing zero. The last block of print statement is not showing anything. I tried using copy.copy by this command 'Access_Point_List.append(copy.copy(i))'. But it's not working.

  • You need to convert all the input coordinates to integers. – Barmar Jan 23 '23 at 18:30
  • All the inputs are strings, you need to convert them to `int`. – Barmar Jan 23 '23 at 18:31
  • Welcome to Stack Overflow. For future reference, please read [ask] and [mre] and https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ and try to narrow down problems before posting. That said, the problem here is an extremely common one. Hint: where the code says `fx, fy, fz = input("Enter coordinates of the first access point: ").split()`, exactly what do you expect this to mean? In particular, what do you think will be the **type** of the `fx` etc. values? Could they ever compare equal to any integer? Why, and how? – Karl Knechtel Jan 23 '23 at 18:31
  • Here's a checklist of how a question like this can be narrowed down to make a proper MRE: * Can we reproduce the problem, if only one set of coordinates is entered - will the list then have the *one* expected result, or nothing at all? * What if we only create one cube, and then have the user input its coordinates? * What if we don't use a class, but only a tuple for the coordinate values? * What if we only ask about the x-coordinate, and only check that coordinate, and only store that coordinate? * What if we remove extra `print` calls - which ones are helping explain the problem? – Karl Knechtel Jan 23 '23 at 18:36
  • @Barmar Thank you Barmar. I can confirm it's working now. I have not used python user input previously. So, that's what caused the problem. Also, I was looking elsewhere for the solution. Much appreciate your reply. – Manash Mahanta Jan 23 '23 at 18:51
  • 1
    @KarlKnechtel Thank you Karl for your comments. "In particular, what do you think will be the type of the fx etc. values?" I was thinking the type will be integers. I am not that much familiar with python user input. Also, thank you for explaining how to narrow down a question. Actually, I already narrowed down the code from my original code. But yes, I could have narrowed down even further. Much appreciate your reply. – Manash Mahanta Jan 23 '23 at 18:55

0 Answers0