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

Alex Diaz
Alex Diaz
4,930 Points

Not sure on how to use random sample

This challenge says I need to use random.sample and i've looked at the docs for it and the docs doesn't explain it in a way I can understand at all. Please help! Thank you

Doc link- https://docs.python.org/2/library/random.html

sample.py
import random


def get_locations(cells):
    pass

1 Answer

Just note that you are reading the Python 2 documentation and the code challenge & Kenneth are both using Python 3.

They are pretty different, so I'd search in the Python 3 documentation.

I found the function in the Python 3 doc on random.sample here. :smile:

Basically, this is how it works. If I were to write this function, I would give is a docstring (think of a docstring as a description of a function) like this:

"""
random.sample(iterable, count)

random.sample returns *count* number of random elements from the iterable *iterable*.

iterable => The iterable you want to get elements out of
count    => The amount of random items you want form the iterable

EXAMPLE:

random.sample([1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 3)

Returns [4, 9, 7]

(This always returns a different value, but this is for an example)
""""

I hope this helps. ~Alex

Alex Diaz
Alex Diaz
4,930 Points

Thank you so much, that challenge was extremely easy now! Lol

No problem :)

Ali Dahud
Ali Dahud
3,459 Points

Thank you so much, teacher ;)