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

Endrin Tushe
Endrin Tushe
4,591 Points

Why isn't my script for nchoices() function working in workspaces?

I am working on this challenge for the Python track:

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 don't understand why my code is not working in workspaces but it works just fine when i enter it into another IDE like Spyder. Could someone help? Am I missing something here? Here is my code:

import random 

def nchoices(my_list, integer):
  new_list = []
  if integer in my_list:
    while len(new_list) < integer:
      new_list.append(random.choice(my_list))
    return new_list
  else:
    print('Sorry {} is not a number found in the list. Try again!'.format(integer)) 

4 Answers

Endrin Tushe
Endrin Tushe
4,591 Points

So I thought I understood the question to mean something along the similar lines and that's what I thought I was doing with my while loop. Basically this is what I understood the question to mean. Say you have a list of integers 1-7. What the question is asking me is this: say I put in integer 5 for my second argument. I need to get back a list of 5 random numbers from my list of 1-7. So that's what I did with my while loop. Basically what I am doing is while the length of my new_list is less than my integer of 5 pick a random number from my list and append it to a new_list. Then once you've done this five times break the while loop and return the new_list of 5 random numbers.

I even put in a condition to restrict the integer to a number from my list. In other words, if someone picks an integer of 50, the which is not one of the integers of my_list 1-7 the while loop will not be executed.

I understand the for loop code that you included in your answers but isn't my while loop doing the same thing? If you test it out with a list and an integer, you will get back exactly a list of 5 random integers or whatever other integer you happen to put for the second argument.

In that case, the conditional statement is completely irrelevant, take the if integer in my_list: and the else: print('Sorry {} is not a number found in the list. Try again!'.format(integer)) out and your code should work just fine.

Hi Endrin, although your code is valid and does not give an error in workspaces, I don't think you understand what the challenge is asking for as you have done something different. What you should have done is create a function that takes 2 arguments (iterable and integer) and selects random items out of your iterable depending on what your integer is. For example, if the integer you passed in was 5, it would select 5 random items from the iterable and return them in a list. Here is what your code should have looked like:

import random

def nchoices(iterable, integer):
  list_of_random_items = []     #create a list where the (integer) amount of random items will be stored
  for i in range(integer):
    list_of_random_items.append(random.choice(iterable)) #choose a random item out of iterable and append it to the list
  return list_of_random_items
Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Perhaps you misread the Task: 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.

This means for integer number of times, select a random item from the iterable. Return selected items as a list

import random 

def nchoices(my_list, integer):
    new_list = []
    for _ in range(integer):  # <-- loop integer times. _ is throw-away variable
        new_list.append(random.choice(my_list))
    return new_list
Endrin Tushe
Endrin Tushe
4,591 Points

Thanks Haider! That did the trick!