2

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

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

Theself.target只是一个元组,self.x并且self.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]
4

3 回答 3

1

您设置self.targetfloatin dist_calc

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)]

于 2013-08-06T22:09:47.990 回答
0
total_dist = (y_dist**2 +x_dist**2)**0.5

float对于 和的大多数整数值,x_dist这将是一个y_dist

self.distance_to_cords[total_dist] = (p.x,p.y)

现在self.distance_to_cords有一个可能的float密钥。

ordering_list = self.distance_to_cords.keys()
ordering_list = sorted(ordering_list)

现在ordering_listlist混合floats 和(偶尔)ints 的一个。

self.target = ordering_list[0]

Nowself.target可能是 a float,这意味着direction_decider如果在底部的代码运行后调用,您可能会引发您声明的异常。

于 2013-08-06T22:11:33.790 回答
0

self.target已经变成了一个float莫名其妙的。

也许self.target = self.target[0]在您的代码中某处发生了类似的事情?

要么,要么self.target = some_variable_that_is_a_float

于 2013-08-06T21:57:55.250 回答