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

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.