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 (Retired) Dungeon Game Random Choices

Create a function named nchoices() that takes an iterable and an integer. The function should return a list of n random


import random

def nchoices(iterable, interger):
    my_list = []

    for index, value in enumerate(iterable):
        return random.choice(my_list),interger

4 Answers

Steven Parker
Steven Parker
217,577 Points

Here are a few hints that may help:

  • you shouldn't have to enumerate the iterable, you just need to pick from it
  • the last line has a typo: the parentheses enclose only the first item instead of all the arguments
  • be sure to build and return a list of random choices, don't just return one choice from the list

Here's a potentially even better hint: There might be an easier way to perform this task. Check out the Python documentation for random and look at the method named sample. Would it be useful here?

And were you aware that there is an updated version of the Python Collections course?

Not helpful at all

import random

def nchoices(iterable, integer):

listi = []
for item in iterable:
    listi.append([item, iterable])

return random.choice(listi)

any help please I don't understand the question

For this, it helps to break it down to what your inputs are, how they should be used/interact with each other and what your expected output is.


  • iterable of any length, with any type(s) of items, from which to randomly select items (including duplicates).
  • positive integer for the number of random items to return

How they are used:

  • loop for n iterations/times, where n is the integer passed in
  • for each iteration of the loop, randomly select an item from the iterable

Expected output:

  • The randomly selected items as a new list (would need a list inside the function to store them in) with length equal to the integer passed in

I don't want to just give you the answer, because then you don't really learn anything, but you will need the following functions/methods:

And there's also a new method in Python 3.6 called random.choices that does exactly what this challenge is asking for, but this course was made well before this was available.

Sneha Nagpaul
Sneha Nagpaul
10,124 Points

example input : [1, 20, 3, 9, 100] , 2 example output : [20, 9]


I/p : "abcdefg", 2 o/p: ['c','g']

Now, you want to make sure if the questions asks for choosing with or without replacement. In the above examples, I am assuming without replacement i.e. the same item cannot be drawn again. This can also be seen as drawing a sample from the collection. For the built in function in the random library, use Steven's suggestion and look up random.sample().

Or, you could write your own--- pick something, remove, repeat until enough choices have been made. Or maybe, pick some random numbers from the indices of the iterable and return those values.

From looking at the code you have posted, you should consider the length of the loop. Do you want to see every value in the iterable? How many values do you want to see? (Does it matter if I see [1, 3, 100] from example 1?)That's how long the loop should be.

Hope this helped.