Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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)