Skip to content

Problems with list comprehensions #57

Open
@nemoyatpeace

Description

@nemoyatpeace

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions