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

alex albas
seal-mask
.a{fill-rule:evenodd;}techdegree
alex albas
Front End Web Development Techdegree Student 2,190 Points

I have a trouble with a list..

In this task , this function takes a list and int number and it returns a list... My question is, what does the first list does? i mean..if the return is a list with a max of numbers, why we need it if we can create a list with random inside the function?

if somebody can explain it me better I will be able to reach the challenge :)

Thanks! Alex

choices.py
import random

def nchoices(llista, num):
  items = random.choice()

3 Answers

William Li
PLUS
William Li
Courses Plus Student 26,868 Points
import random

def nchoices(llista, num):

what you should do in the function body is

  • first create an empty list to hold the result
  • pick a random number from llista, append it to the result list, keep doing that num times.
  • after that, return the result list as the return value of the function.
William Li
William Li
Courses Plus Student 26,868 Points

alex, pick a random number from llista by doing this

random.choice(llista)

That will return you 1 randomly picked element from llista