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
Alex Trost
6,270 PointsI can't figure out why I'm getting a TypeError some of the time when I run my dungeon game.
Been hitting my head against this for a couple hours now. Any help is appreciated.
Sometimes the game runs just fine, but other times I get the following error: " p, d, m1, m2, m3 = starting_rooms() TypeError: 'NoneType' object is not iterable" So it has to be something to do with what is being randomly picked, but I can't figure it out.
I've tried to plug in try/except in both of these functions, but it kept catching the same thing.
import random
max_rooms=4
# Generate the tuples
LIST_XY=[]
x = list(range(max_rooms))
y = list(range(max_rooms))
for i in x:
for t in y:
LIST_XY.append((i,t))
def starting_rooms():
# Defines the starting rooms for p, player; d, door; m1, monster 1; m2, monster 2; and m3, monster 3
global p, d, m1, m2, m3
p = random.choice(LIST_XY)
d =random.choice(LIST_XY)
m1 =random.choice(LIST_XY)
m2 =random.choice(LIST_XY)
m3 =random.choice(LIST_XY)
if p == d or p == m1 or p == m2 or p == m3 or d == m1 or d == m2 or d == m3 or m1 == m2 or m1 == m3 or m2 == m3:
starting_rooms()
else:
return p,d,m1,m2,m3
def generate_room_dictionary():
room_dict={}
for room in LIST_XY:
if room == m1:
room_dict[room] = 'monster1'
elif room == m2:
room_dict[room] = 'monster2'
elif room == m3:
room_dict[room] = 'monster2'
elif room == p:
room_dict[room] = 'player'
elif room == d:
room_dict[room] = 'door'
else:
room_dict[room] = 'empty'
return room_dict
p, d, m1, m2, m3 = starting_rooms()
room_dict = generate_room_dictionary()
print(room_dict) # Print is only here to show if it's working. It's temporary and won't be used in the final program.
There's a lot more code in functions that takes place after this, as this just sets up the game, but this is where I'm hitting the error, so I'm just including this.
And yeah, I added 'global', even though I know I've been warned against it... Not sure how else to really handle the tidy passing back and forth of variables between all these functions without the shortcut. It was throwing up the type error before that, though. That was an attempted workaround.
Any advice would be fantastic. Thanks!
1 Answer
Steven Parker
243,227 PointsThe error occurs when starting_rooms does a re-try, and in that case it doesn't return anything. It should return the result of the re-try, like this:
if p == d or p == m1 or p == m2 or p == m3 or d == m1 or #... etc
return starting_rooms() # return the result of the re-try
else:
return p,d,m1,m2,m3
And, yea, get rid of the global line. Otherwise, you should eliminate the returns and the assignment when it is called.
Alex Trost
6,270 PointsAlex Trost
6,270 PointsAh! It took me a minute to understand why we'd return with a function, but it just takes it a bit deeper, runs a second iteration of starting_rooms() then when that starting_rooms() returns variables, we should return those returned variables.
Am I basically understanding it?
It works perfectly now, thanks so much!
I'm doing global because otherwise, to me at least, it seems like I need to have functions that take a ton of arguments, and the whole thing gets very confusing with all the variables flying back and forth. Any suggestions on avoiding all of that without going global?
Steven Parker
243,227 PointsSteven Parker
243,227 PointsAfter making that correction, the variables will all be passed back correctly. Just delete the global line.