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
Paxton Butler
6,338 PointsNoneType Error in Dungeon Game
I keep getting the following error every third or fourth time I run the dungeon game:
Traceback (most recent call last):
File "dungeon.py", line 71, in <module>
monster, door, player = get_locations()
TypeError: 'NoneType' object is not iterable
I think it is because something is wrong with how I call the get_locations() method in the if statement, but I can't figure it out!!
Here is my code:
def get_locations():
monster = random.choice(CELLS)
door = random.choice(CELLS)
start = random.choice(CELLS)
if monster == door or monster == start or start == door:
get_locations()
else:
return monster, door, start
...
monster, door, player = get_locations()
move_history = []
move_history.append(player)
print("Welcome to the dungeon!")
print(monster)
print(door)
print(move_history)
1 Answer
Mikael Enarsson
7,056 PointsIt's a bit hard to tell, and the complete code would be more useful. However, there is one thing that stands out to me:
def get_locations():
monster = random.choice(CELLS)
door = random.choice(CELLS)
start = random.choice(CELLS)
if monster == door or monster == start or start == door:
get_locations() #Here, I'm fairly certain that you have to have
#return, in order to return the thing
#returned from the function to the
#thing that called the function
#in the first place.
else:
return monster, door, start
I could imagine that it's that, or maybe you haven't properly initialized CELLS?
Kenneth Love
Treehouse Guest TeacherMy money, seeing just this snippet of code, would be that, yes, they need a return. Otherwise it just calls the function and nothing actually goes back to the code that wanted the values in the first place.
Tobias Edwards
14,458 PointsTobias Edwards
14,458 PointsCan you post all your code, please?