This is the error I'm getting from this function:
这是我从这个函数中得到的误差
TypeError: 'float' object has no attribute '__getitem__'
The self.target
is just an tuple and self.x
and self.y
are ints, I don't know what I've done wrong.
自我。目标只是一个元组和自我。x和自我。我不知道我做错了什么。
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]
3 个解决方案
#1
1
You set self.target
to a float
in dist_calc
:
你的自我。目标在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]
Here ordering_list
is a sequence of floating point values (the total_dist
values), and you set self.target
to the lowest value of these (you could have used min(self.distance_to_cords)
there instead of sorting).
这里ordering_list是一个浮点值序列(total_dist值),然后设置self。以这些值的最小值为目标(您可以在那里使用min(self. distance_to_lines)而不是排序)。
Perhaps you meant to set it to self.distance_to_cords[min(self.distance_to_cords)]
instead?
也许你想把它设置为self。distance_to_lines [min(self. distance_to_lines)] ?
#2
0
self.target
has become a float
somehow.
自我。目标变成了一个浮动。
Perhaps something like self.target = self.target[0]
happened somewhere in your code?
也许类似的自我。目标=自我。目标[0]发生在代码中的什么地方?
Either that, or self.target = some_variable_that_is_a_float
,或自我。目标= some_variable_that_is_a_float
#3
0
total_dist = (y_dist**2 +x_dist**2)**0.5
This will be a float
for most integer values of x_dist
and y_dist
.
这将是x_dist和y_dist的大多数整数值的浮点数。
self.distance_to_cords[total_dist] = (p.x,p.y)
Now self.distance_to_cords
has a probable float
key.
现在的自己。distance_to_lines具有一个可能的浮动键。
ordering_list = self.distance_to_cords.keys()
ordering_list = sorted(ordering_list)
Now ordering_list
is a list
of mixed float
s and (occasionally) int
s.
现在ordering_list是混合浮点数和(偶尔)ints的列表。
self.target = ordering_list[0]
Now self.target
is probably a float
, which means that your direction_decider
would likely raise the exception you stated if called after your code at the bottom runs.
现在的自己。target可能是一个浮点数,这意味着您的direction_decider可能会在底层运行的代码之后调用您所声明的异常。
#1
1
You set self.target
to a float
in dist_calc
:
你的自我。目标在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]
Here ordering_list
is a sequence of floating point values (the total_dist
values), and you set self.target
to the lowest value of these (you could have used min(self.distance_to_cords)
there instead of sorting).
这里ordering_list是一个浮点值序列(total_dist值),然后设置self。以这些值的最小值为目标(您可以在那里使用min(self. distance_to_lines)而不是排序)。
Perhaps you meant to set it to self.distance_to_cords[min(self.distance_to_cords)]
instead?
也许你想把它设置为self。distance_to_lines [min(self. distance_to_lines)] ?
#2
0
self.target
has become a float
somehow.
自我。目标变成了一个浮动。
Perhaps something like self.target = self.target[0]
happened somewhere in your code?
也许类似的自我。目标=自我。目标[0]发生在代码中的什么地方?
Either that, or self.target = some_variable_that_is_a_float
,或自我。目标= some_variable_that_is_a_float
#3
0
total_dist = (y_dist**2 +x_dist**2)**0.5
This will be a float
for most integer values of x_dist
and y_dist
.
这将是x_dist和y_dist的大多数整数值的浮点数。
self.distance_to_cords[total_dist] = (p.x,p.y)
Now self.distance_to_cords
has a probable float
key.
现在的自己。distance_to_lines具有一个可能的浮动键。
ordering_list = self.distance_to_cords.keys()
ordering_list = sorted(ordering_list)
Now ordering_list
is a list
of mixed float
s and (occasionally) int
s.
现在ordering_list是混合浮点数和(偶尔)ints的列表。
self.target = ordering_list[0]
Now self.target
is probably a float
, which means that your direction_decider
would likely raise the exception you stated if called after your code at the bottom runs.
现在的自己。target可能是一个浮点数,这意味着您的direction_decider可能会在底层运行的代码之后调用您所声明的异常。