Python float对象没有属性uu getitem__
这是我从这个函数中得到的错误:Python float对象没有属性uu getitem__,python,types,pygame,Python,Types,Pygame,这是我从这个函数中得到的错误: TypeError: 'float' object has no attribute '__getitem__' self.target只是一个元组,self.x和self.y是整数,我不知道我做错了什么 class Robot(object): def __init__(self): global WIDTH global HEIGHT global BACKGROUND self.speed = random.randint(
TypeError: 'float' object has no attribute '__getitem__'
self.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]
类机器人(对象):
定义初始化(自):
全局宽度
全球高度
全球背景
self.speed=random.randint(0,8)
self.size=5
self.counter=0
self.direction=“n”
self.target=(0,0)
方向=[“n”、“ne”、“e”、“se”、“s”、“sw”、“w”、“nw”、“stop”]
self.distance_to_cords={}
self.target_跳线=[]
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,宽度)
self.y=random.randint(0,高度)
self.color=红色
def牵引(自):
pygame.draw.polygon(DISPLAYSURF,self.color,((self.x,self.y+self.size),(self.x,self.y+self.size,self.y+self.size),(self.x+self.size,self.y)))
pygame.display.update()
def undraw(自身):
pygame.draw.polygon(显示冲浪,背景,((self.x,self.y),(self.x,self.y+self.size),(self.x+self.size,self.y+self.size),(self.x+self.y+self.y)))
pygame.display.update()
def方向_决策器(自身):
#x东西
#w
如果self.target[0]self.x:
问题2=正确
其他:
问题2=错误
#n
如果self.target[0]self.y:
问题4=正确
其他:
问题4=错误
答案=(问题1、问题2、问题3、问题4)
查找\u which\u direct={(真、假、假、假):“w”,
(假,真,假,假):“e”,
(假,假,真,假):“n”,
(假,假,假,真):“s”,
(对,错,对,错):“nw”,
(真、假、假、真):“sw”,
(假,真,真,假):“ne”,
(假,真,假,真):“se”}
cheese=查找\u哪个\u直接[回答]
印花奶酪
def dist_calc(自身):
对于工厂清单中的p:
x_dist=self.x-p.x
y_dist=self.y-p.y
总距离=(y距离**2+x距离**2)**0.5
自我距离[总距离]=(p.x,p.y)
排序\u列表=self.distance\u到\u跳线.keys()
排序列表=已排序(排序列表)
self.target=排序列表[0]
self.target_跳线=self.distance_到_跳线[self.target]
self.target
在某种程度上已经变成了一个浮动
您的代码中可能发生了类似于self.target=self.target[0]
的情况
或者self.target=某个变量是浮动的self.target您在dist\u 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\u list
是一系列浮点值(total\u dist
值),您可以将self.target
设置为这些值中的最低值(您可以在那里使用min(self.distance\u-to\u-cords)
而不是排序)
也许您想将其设置为self.distance\u-to\u-cords[min(self.distance\u-to\u-cords)]
total_dist = (y_dist**2 +x_dist**2)**0.5
对于x_dist
和y_dist
的大多数整数值,这将是一个float
self.distance_to_cords[total_dist] = (p.x,p.y)
现在,self.distance\u-to\u-cords
可能有一个float
键
ordering_list = self.distance_to_cords.keys()
ordering_list = sorted(ordering_list)
现在,ordering\u list
是一个混合float
s和(偶尔)int
s的列表
self.target = ordering_list[0]
现在,self.target
可能是一个float
,这意味着您的direction\u决策器
如果在底部运行代码后调用,可能会引发您所述的异常。那么什么是self.target
?我的第一个想法是,这一定是这里的float
值。什么是完全回溯?为什么不显示错误的完全回溯?如果失败发生在self.target
上,那么如果您不显示它的定义位置(并且:不,如果出现错误,它不是一个元组),我们如何知道它失败的原因。(旁注:如果条件为:questionX=True,否则questionX=False,而不是if-condition:questionX=True
,只要做questionX=condition
,或者not condition
,如果你想要否定的话。)请稍等,我会快速发布我所有的代码,似乎self.target
是一个float
和self.target\u是一个元组。也许你想在使用前者的地方使用后者。我不明白你为什么要为你的问题选择任何答案。这些人正试图帮助解决你的问题。如果还缺少什么,请向他们解释。你也得到了投票的答案,所以如果你问我,你的行为有点奇怪。