filbert
filbert copied to clipboard
Problems with list comprehensions
I was attempting these lines of code:
self.say(len(coins))
bottom = [i for i in coins if i.pos.y <= 51-(13/17)*abs(i.pos.x-60)]
coins = [i for i in bottom if i.pos.x>self.pos.x]
self.say(len(coins))
the first self.say worked fine, the second was broken.
I tried breaking both of the comprehensions into simple for loops and it worked fine:
bottom = []
for i in coins:
if i.pos.y <= 51-(13/17)*abs(i.pos.x-60):
bottom.append(i)
coins = []
for i in bottom:
if i.pos.x>self.pos.x:
coins.append(i)
When I tried it with the first comprehension and the second as a loop I got an error on i.pos.x complaining the null doesn't have a .pos.
bottom = [i for i in coins if i.pos.y <= 51-(13/17)*abs(i.pos.x-60)]
coins = []
for i in bottom:
if i.pos.x>self.pos.x:
coins.append(i)
I tried the other direction with the bottom as a list and the coins as a comprehension:
bottom = []
for i in coins:
if i.pos.y <= 51-(13/17)*abs(i.pos.x-60):
bottom.append(i)
coins = [i for i in bottom if i.pos.x>self.pos.x]
And it worked. So apparently the issue is in the first comprehension.
I'm going to guess that this is the same as the other bug with reusing list comprehension variable names: https://github.com/codecombat/aether/issues/133