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

Blake Handson
Blake Handson
1,642 Points

Why won't this print out my results in the console? Just testing this out in console not submitting for the challenge.

So, i'm testing this in the console to see if it actually works. Right now i'm not getting any errors but nothing is print arghhh! Can someone point me in the right direction?

Thanks, B.

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

iterable = [1,2,3,4,5,6,7,8,9]
n = 10


def nchoices (iterable, n):

  ##take an interable and an integer and return a list
  #of n random items from the iterable, where n is the integer.
  ##Duplicates are allowed

  #Initialize empty list for results  
  results = [] 

  for index in range(n): #<-- loop through the iterable n number of times
                        #<-- this will return n items from the iterable! Makes sense

      pick = random.choice(iterable) ##pick random item during the loop instance
      result.append(pick) ##append pick to results list

      print (results)       
  return (results)

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

You've defined a function called nchoices but you still need to call it to get the code to execute.