【问题标题】:float object has no attribute __getitem__浮动对象没有属性 __getitem__
【发布时间】:2013-08-07 06:11:33
【问题描述】:

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

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 是什么?我的第一个想法是 that 必须是这里的 float 值。什么是完整的回溯?
  • 为什么不显示错误的完整回溯?如果失败是self.target,那么如果你不显示它的定义位置,我们应该如何知道它失败的原因(并且:不,如果你得到一个错误,它不是一个元组)。 (旁注:而不是if condition: questionX = True else questionX = False,只需使用questionX = condition,或者如果您想要否定,则使用not condition。)
  • 等一下,对不起,我把我所有的代码都贴出来了
  • 看一眼,self.target 似乎是一个 floatself.target_cords 是一个元组。也许您想在使用前者的地方使用后者。
  • 我不明白您为什么选择任何答案来回答您的问题。这些人正试图帮助您解决问题。如果仍然缺少某些东西,请向他们解释。你也得到了赞成的答案,所以如果你问我,你的行为有点奇怪。

标签: python types pygame


【解决方案1】:

您在dist_calc 中将self.target 设置为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)]

【讨论】:

    【解决方案2】:

    self.target 不知何故变成了float

    也许像self.target = self.target[0] 这样的事情发生在你的代码中?

    要么,要么self.target = some_variable_that_is_a_float

    【讨论】:

      【解决方案3】:
      total_dist = (y_dist**2 +x_dist**2)**0.5
      

      对于x_disty_dist 的大多数整数值,这将是float

      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]
      

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

      【讨论】:

        猜你喜欢
        • 2013-05-12
        • 2013-03-17
        • 2015-10-26
        • 2014-08-14
        • 1970-01-01
        • 1970-01-01
        • 2012-12-04
        • 2012-10-15
        • 2014-01-16
        相关资源
        最近更新 更多