Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (Retired) Dungeon Game Building the Game: Part 2

Pepe Suarez
Pepe Suarez
18,267 Points

Can't Fix Bug....

Hey Guys I am Getting the following error from time to time in the game:

Traceback (most recent call last): File "C:\Users\Pepe\game.py", line 25, in door,monster,player = get_locations() TypeError: 'NoneType' object is not iterable

this is how my function looks like:

``` def get_locations(): door = random.choice(MAP) monster = random.choice(MAP) player = random.choice(MAP)

if door == monster or door == player or player == monster:
    get_locations()
else:
    return door,monster,player

door,monster,player = get_locations() ```

The game works most of the times, but I don't know if using the random.choice() method is returning a NoneType value as a choice... Thanks for the help!!! : )

Pepe Suarez
Pepe Suarez
18,267 Points

Sorry Didn't Format the code well:

def get_locations():
    door = random.choice(MAP)
    monster = random.choice(MAP)
    player = random.choice(MAP)

    if door == monster or door == player or player == monster:
        get_locations()
    else:
        return door,monster,player



door,monster,player = get_locations()

And this is how my MAP looks like:

MAP = [(1,1),(1,2),(1,3),
       (2,1),(2,2),(2,3),
       (3,1),(3,2),(3,3)
]

1 Answer

Martin Cornejo Saavedra
Martin Cornejo Saavedra
18,132 Points

Looks like you are calling MAP inside the functions scope, but it doesn't exist inside that scope, it exist in the global scope. I'd do this change and see if this works:

def get_locations(any_MAP):
    door = random.choice(any_MAP)
    monster = random.choice(any_MAP)
    player = random.choice(any_MAP)

    if door == monster or door == player or player == monster:
        get_locations(any_MAP)
    else:
        return door,monster,player
MAP = [(1,1),(1,2),(1,3),
       (2,1),(2,2),(2,3),
       (3,1),(3,2),(3,3)
]
door,monster,player = get_locations(MAP)