python - float 对象没有属性 __getitem__

标签 python types pygame

这是我从此函数中得到的错误:

TypeError: 'float' object has no attribute '__getitem__'

self.target 只是一个元组,self.xself.y 是整数,我不知道我是什么做错了。

class Robot(object):
def __init__(self):
    global WIDTH
    global HEIGHT
    global BACKGROUND
    self.speed = random.randint(0,8)
    self.size = 5
    self.counter = 0
    self.direction = "n"
    self.target = (0,0)
    self.directions = ["n","ne","e","se","s","sw","w","nw","stop"]
    self.distance_to_cords = {}
    self.target_cords = []

    self.direction_movementsy = {"n": -1,
                                "ne" : -1,
                                "e" : 0,
                                "se" : 1,
                                "s": 1,
                                "sw": 1,
                                "w": 0,
                                "nw": -1}

    self.direction_movementsx = {"n": 0,
                                "ne" : 1,
                                "e" : 1,
                                "se" : 1,
                                "s": 0,
                                "sw": -1,
                                "w": -1,
                                "nw": -1}







    self.x = random.randint(0,WIDTH)
    self.y = random.randint(0,HEIGHT)
    self.colour = RED

def draw(self):
    pygame.draw.polygon(DISPLAYSURF,self.colour,((self.x,self.y),(self.x,self.y + self.size ),(self.x + self.size,self.y + self.size),(self.x + self.size,self.y)))
    pygame.display.update()

def undraw(self):
    pygame.draw.polygon(DISPLAYSURF,BACKGROUND,((self.x,self.y),(self.x,self.y + self.size ),(self.x + self.size,self.y + self.size),(self.x + self.size,self.y)))
    pygame.display.update()

def direction_decider(self):
    #x stuff

    #w
    if self.target[0] < self.x:
        question1 = True
    else:
        question1 = False

    #e
    if self.target[0] > self.x:
        question2 = True
    else:
        question2 = False


    #n
    if self.target[0] < self.y: 
        question3 = True
    else:
        question3 = False

    #s
    if self.target[0] > self.y:
        question4 = True
    else:
        question4 = False


    answer = (question1, question2, question3, question4)


    lookup_which_direct = { (True,False,False,False):"w",
                            (False,True,False,False):"e",
                            (False,False,True,False):"n",
                            (False,False,False,True):"s",
                            (True,False,True,False):"nw",
                            (True,False,False,True):"sw",
                            (False,True,True,False):"ne",
                            (False,True,False,True):"se"}

    cheese =lookup_which_direct[answer]
    print cheese












def dist_calc(self):
    for p in plant_list:
        x_dist = self.x - p.x
        y_dist = self.y - p.y
        total_dist = (y_dist**2 +x_dist**2)**0.5
        self.distance_to_cords[total_dist] = (p.x,p.y)
    ordering_list = self.distance_to_cords.keys()
    ordering_list = sorted(ordering_list)
    self.target = ordering_list[0]
    self.target_cords = self.distance_to_cords[self.target]

最佳答案

您将 self.target 设置为 dist_calc 中的 float:

for p in plant_list:
    x_dist = self.x - p.x
    y_dist = self.y - p.y
    total_dist = (y_dist**2 +x_dist**2)**0.5
    self.distance_to_cords[total_dist] = (p.x,p.y)
ordering_list = self.distance_to_cords.keys()
ordering_list = sorted(ordering_list)
self.target = ordering_list[0]

此处 ordering_list 是浮点值序列(total_dist 值),您将 self.target 设置为这些(您可以在那里使用 min(self.distance_to_cords) 而不是排序)。

也许您打算将其设置为 self.distance_to_cords[min(self.distance_to_cords)]

关于python - float 对象没有属性 __getitem__,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18091109/

相关文章:

c - 当指针未指向正确的类型时,为什么 gcc 允许取消引用。为什么还要有类型?

python - 从 Pygame 中的组中获取特定 Sprite

python - pygame的安装

python - 处理上传图片 django admin python

python - 代码不从字典中删除所需的值

Java 和 python ^ 运算符

c++ - 对整数类型进行舍入,但不对小数类型进行舍入

java - Android应用与树莓派通信

javascript - 从对象类型的字符串值创建字符串文字类型

python - pygame中Tiled和pytmx的透明图 block 问题