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 trialBrian York
6,213 PointsQuestion about parameters (dungeon game)
Hi,
I have a question. While defining a function that returns three random unique elements of an iterable (to be used as locations) I came across a problem.
Kenneth uses this code: """def get_locations(): return random.sample(CELLS, 3)"""
Then when calling the function he does this:
"""monster, door, player = get_locations()"""
This is the code I came up with before watching the video (but it doesn't work):
"def get_locations(CELLS): (monster, player, door) = random.sample(CELLS, 3) return (monster, door, player)"
Then when I call the function I do this: """get_locations(CELLS)"""
My questions are, why does my code not work? And, more crucially, why does Kennth's version not need CELLS as a parameter for the function? Since he is explicitly using that information, wouldn't the function need that information as an input to run properly?
Thanks for the help in advance.
2 Answers
James J. McCombie
Python Web Development Techdegree Graduate 21,199 PointsThere is a lot to answer in this so bear with me.
There is a question of scope. CELLS is defined globally in the script. From what I remember of this tutorial, CELLS is a list of 2-tuples, like (1, 0). A function has access to globally defined variables, so there is no need to pass it to the function to operate on it. The function get locations, using random.sample(CELLS, 3) returns a list of three tuples in this case, the statement monster, player, door = get_locations(), means that the return of the function, the list, gets unpacked sequentially and assigned in turn to the variables monster, player and door. I think simply your function is too busy you are creating a tuple of three tuples, and returning that tuple to be unpacked into three variables.
Your code should in principle work, you end up returning a tuple (monster, player, door), which you could then use as it is and slice into it to get at the contents, or unpack it again.
When you define a function, a_function(), what you put in the parentheses are the arguments for the function. Writing CELLS in the get_location function () does not guarantee that CELLS will be passed into the function.
def get_locations(CELLS):
means that the get_locations function takes an argument called CELLS, but you could pass anything in when calling the function, say an integer. Now in your case since CELLS is defined globally, if you pass CELLS into the function that takes an argument called CELLS, then you will have CELLS used in the function, but you could also have defined something called new_cells which is an identical list of 2-tuples, and called get_locations(new_cells) and seen the same output.
you could use a keyword argument in the defining of the function, get_locations(CELLS=CELLS), then when you called get_locations(), without any arguments, you would also see the same output.
let me know if I can explain that better....
Brian York
6,213 PointsI think that I get all of that. Thanks. I'm fairly new to coding, and I can usually complete the problems, but often its not because I really understand all of the tools and logic underlying my code. I'm trying to understand more of that. Thanks for the answer.