0

2 questions: why is 'x' undefined? And is there some standard elegant solution to 2d coordinate lookup that isn't this mess?

I am new to programming and learning on my own, and have weak research skills. Its hard to look something up when you don't know its name.

What I am trying to do: Make a game that uses 2-d coordinates (x,y). To accomplish that, I have created the following function that doesn't work. I know it doesn't work... I don't understand why. I can fix it by removing its flexibility of input, and creating duplicate functions which seems like a bad solution.

My [Python] code

class Wall:
    def __init__(self, x="", y=""):
        self.x = x
        self.y = y

foo = []
foo.append(Wall(x=1, y=3))


def return_index(list_input, x_coord, y_coord, class_x, class_y):
    for i in range(len(list_input)):
        if list_input.class_x is x_coord:
            if list_input.class_y is y_coord:
                print("return i = ",i)
                return (i)

print(foo[1].x)

return_index(list_input=foo, x_coord=1, y_coord=2, class_x=x, class_y=y)

error code:

Traceback (most recent call last):
File "E:/Programing Python/platformer/test.py", line 19, in <module>
return_index(list_input=foo, x_coord=1, y_coord=2, class_x=x, class_y=y)
NameError: name 'x' is not defined

Psudocode:

list with class instances
each class instance contains x & y coordinates. 
(each x,y pair is unique and does not repeat)

go through list, i = current list index:
    if list[i].class_x is x_coord:
        if list[i].class_y is y_coord:
             return [i]  # return list index of class instance containing target x,y pair
  • 2
    `foo` only has 1 item so use `foo[0].x`, where do you define `x` or `y` that you pass to `return_index`? – depperm Mar 19 '18 at 17:36
  • 2
    For starters: to test for value equality, use `==`, not `is`. `is` only tests for *identity*, it being the same object. That may work for small integers but that's a coincidence, not the norm. Use `if list_input.class_x == x_coord and list_input.class_y == y_coord:` (combining the two `if` statements into one using `and`). – Martijn Pieters Mar 19 '18 at 17:36
  • What would `class_x=x, class_y=y` *do*? I suspect you wanted to name attributes. You can do so using *strings*, then using `getarr()` to retrieve the attribute named. – Martijn Pieters Mar 19 '18 at 17:38
  • From looking at your code, you are trying to store a collection of Walls that will have an x coord, a y coord, both integers, and then some details about the wall eventually, and you are trying to access them by the x,y value? In the future do you need the collection in a particular order, or will you only ever be accessing the data randomly? I.E. will you want to get all the walls where x < 5 and y < 5 at some point. This would dictate the best way to store the data. – Michael Robellard Mar 19 '18 at 17:46
  • Thank you Martijn Peters, I didn't know about getattr() and it seems perfect for solving my problem. What I thought class_x=x, class_y=y would do is allow me to define what class attribute I was trying to pull a value from. which seems to be what getattr() is for. – Daniel B. Mar 19 '18 at 17:49
  • @MichaelRobellard I was trying to provide a simple example of my problem. I don't know enough to have any idea what I don't know yet so please bear with me. I think I will need to both access data based on adjacency and x <5 and y < 5, as well as point information. I have no idea what will happen the most. There will be more information tacked onto walls eventually. Is there a website that explains how to pick/use data structures for simple people? – Daniel B. Mar 19 '18 at 18:02

0 Answers0