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

Nicholas Anstis
Nicholas Anstis
2,095 Points

Python collections random.choice challange

Here's what i have to do

Create a function named nchoices() that takes an iterable and an integer. The function should return a list of n random items from the iterable where n is the integer. Duplicates are allowed.

I can't get it right if anybody could help me out :D

import random

n = list(range(50))
def nchoices(iter1, integer):
  return random.choice(n), n = integer 

1 Answer

John Lindsey
John Lindsey
15,641 Points

Hi Nicholas,

I have the answer below. So first, you imported random. This will help us to get our random integer that we will use as an index for our iterable. I did "from random import randint" because this allows me to use randint without typing random.randint. If you just want to use "import random", then just put "random." in front of randint. Next we will define the function choices and give it too parameters (I named them iterable and integ). It wants us to return a list so I created an empty list at the beginning of the function. The problem is asking for us to return random items from the iterable we passed it. An example would be, say we passed nchoices "hello" as the iterable and 3 as the integ. This would mean that our list would contain 3 letters from "hello" (and the same letter could appear more than once). So we create a for loop. I used and underscore in my for loop statement because we will not need to reference it anywhere. It is just being used as a counter. We will want the for loop to iterate as many times as we said in the integ parameter (in our example 3). This is done with the range(integ). Then we just extend the list. In the extend we will want to use a random number as our index for the iterable. So in the brackets after our iterable variable, we put "randint(0, len(iterable)-1)". This will generate a random integer from 0 to one less than the length of the iterable so we can get every possible letter. Then all we have to do is return the list that we created after the for loop has completed. Hope this helps!

from random import randint

def nchoices(iterable, integ):
    return_list = []
    for _ in range(integ):
        return_list.extend(iterable[randint(0, len(iterable)-1)])
    return return_list
10,908 Points

Thank you for your clear explanation of "why". it helped me on a different task I was confused with.