创建灵活的函数,'x'未定义错误

时间:2018-03-19 17:34:48

标签: python python-3.x

2个问题: 为什么'x'未定义?对于二维坐标查找是否有一些标准的优雅解决方案,这不是一团糟吗?

我是自己编程和学习的新手,并且研究技巧很差。当你不知道它的名字时很难看清楚。

我想做什么: 制作一个使用二维坐标(x,y)的游戏。为了实现这一点,我创建了以下无效的功能。我知道它不起作用......我不明白为什么。 我可以通过删除它的输入灵活性和创建重复的功能来解决它​​,这似乎是一个糟糕的解决方案。

我的[Python]代码

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)

错误代码:

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

0 个答案:

没有答案
相关问题