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 trialConrad Landicho
3,312 Pointsrandom.sample()
Hi everyone, I'm working on this Challenge Question that goes like this (it's from the dungeon game): I haven't shown you how to use this function yet but I'm sure you can use it. In the random library, there's a function named sample that takes two arguments: an iterable to sample from, and an integer of how many unique samples to return. Finish the get_locations function so that it returns 3 unique values from the cells argument. I don't quite understand this question. So, what I did was to look at the documentation of random.sample(), but still, I'm not sure how this relates to the Challenge Question.
import random
def get_locations(cells):
# pass
return x, y, z
3 Answers
Conrad Landicho
3,312 PointsI think I got it :-) Thanks guys :-)
Here's what I did, and it passed :-)
def get_locations(cells): return random.sample(cells, 3)
rhupp
11,019 PointsThe pass
part of the function is what threw me off. It felt a little like a trick question, to be honest. Glad to be through it!
Cole Wilson
7,413 PointsRemember that you can return tuples in Python.
http://stackoverflow.com/questions/354883/how-do-you-return-multiple-values-in-python
So all you need to do is set (x, y, z) to the output of random.sample(arg1, arg2).
arg1 and arg2 are listed as part of the question.
Alexander Davison
65,469 PointsActually in Conrado Landicho's he is returning a tuple.
Cole Wilson
7,413 PointsCole Wilson
7,413 PointsNice! It's tough to decide how much to share.
Great job figuring it out!
maxwell ruzvidzo
6,822 Pointsmaxwell ruzvidzo
6,822 Pointsthanks for the answer