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 trialNate Spry
Courses Plus Student 2,103 PointsCan someone explain why "cells_creator" is not defined? (Expansion of dungeon_game.py)
I'm trying to expand the dungeon_game.py file that we created with Kenneth at the end of Python Collections. Independently, my functions work but I'm having trouble when I try to assign them to the variable "CELLS". Here is my code:
CELLS = cells_creator(difficulty)
def cells_creator(difficulty):
range_list = []
for i in range(difficulty):
range_list.append(i)
index_tracker = 0
cells = []
complete = False
while not complete:
for item in range_list:
cells.append(tuple((item, range_list[index_tracker])))
index_tracker += 1
if index_tracker > range_list[-1]:
complete = True
return cells
def difficulty_selector():
difficulty = input("What difficulty would you prefer? EASY, MEDIUM, HARD ").upper()
if difficulty == "EASY":
difficulty = 5
if difficulty == "MEDIUM" or "HARD":
difficulty = 10
else:
input("Sorry, that was not a valid entry: press ENTER to try again!")
return difficulty
My error is: Traceback (most recent call last): File "C:\Users\Alex\AppData\Local\Programs\Python\Python37-32\PythonFiles\dungeon_game_copy.py", line 4, in <module> CELLS = cells_creator(difficulty) NameError: name 'cells_creator' is not defined
I'm assuming it has something to do with how I have my functions ordered but I don't know why. If someone could explain it to me that would be great!
1 Answer
Dave StSomeWhere
19,870 PointsYou can't assign cells_creator(difficulty)
to a variable until after the function is defined.
Also you can use difficulty
as an argument until after it is assigned a value.
Try adding an assignment to difficulty
and the call to cells_creator()
at the bottom after the function definitions.
Nate Spry
Courses Plus Student 2,103 PointsNate Spry
Courses Plus Student 2,103 PointsSo, I moved everything around but now I get:
NameError: name 'difficulty' is not defined. I don't understand why because I defined difficulty within difficulty_selector. This time, I've added my entire code just in case it's something further down the line that's tripping things up. Apologies if that is too much.
Dave StSomeWhere
19,870 PointsDave StSomeWhere
19,870 Pointsdifficulty is only defined within the difficulty_selector() function scope and you are attempting to use it in the global scope. So you should probably investigate variable scoping.
Try this - added some comments - this should get you rolling - seems to work - didn't really do complete testing
Nate Spry
Courses Plus Student 2,103 PointsNate Spry
Courses Plus Student 2,103 PointsThank you so much for your help. I've yet to explore local and global variables and will hopefully get there soon. It makes more sense to me now!