Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (2016, retired 2019) Dungeon Game random.sample

samuel fiori
samuel fiori
4,497 Points

What's the problem on task: "sample.py" ?? On my IDLE it runs perfect.

The Task is to "Finish the get_locations function so that it returns 3 unique values from the cells argument."

sample.py
import random


def get_locations(cells):
    return sample(cells, 3)

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! I'm not exactly sure why it runs on what you're using, but the sample function should be run on random which you imported earlier. This is my line that includes the sample function.

    return random.sample(cells, 3)

Hope this helps! :sparkles:

samuel fiori
samuel fiori
4,497 Points

Yeah´, but on my IDLE I used "from random import sample" and with this method I get an NameError because random is not defined so I guess I have to use "random.sample()" when I used "import random" and otherwise just "sample()". That would make sense. If not please correct.

In all cases thank's for help!

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

That would make sense as you imported the sample function from the random library instead of the entire library of random. Random has a lot of available functions. Take a look here. I believe that if you import a function directly, you don't need to specify that it's related to the random class. But you could have had a sample function in your own code and then wanted to use some other function in the random library. In that case, you'd have to specify that it's the sample function in the random library.