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 collections. random choice. challenge task 1
I swear to go is it me or are the code challenges and the video lectures before each challenge are a quantum leap in difficulty?
I mean hey. the random choice lecture was 2 minutes long explaining random choice and now the code challenge wants multiple random choices from an iterable where it was never even mentioned in the lecture how to do so.
am I missing something?
lists = 
Chris FreemanTreehouse Moderator 68,423 Points
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.
Modifying your code slightly,
Edit: updated code for clarity
def nchoices(iterable, n):
'''Take an iterable 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): #<-- Add loop to run "n" times
# pick random item
pick = random.choice(iterable)
# append pick to results list
results.append(pick) #<-- in each loop, choose random item from "iterable" and append to results list
# after looping complete, return results list
Nick Miller4,417 Points
I have to somewhat agree with the OP's statement about the code challenges often being a lot more complex than their accompanying videos. I understand that the code challenges are a good opportunity to apply everything we've learned (not just the one video). That being said, some of these challenges can be pretty difficult. The OP is right, we went from a quick video that explained one simple function to a code challenge that required us to apply several advanced (for us) topics.
Speaking of which, when did we learn how to loop through a function
n times? Even if we did learn how to do that, I'm sure we didn't learn how to loop through something
n times where
n is equal to a function's argument.
Peter Rzepka4,118 Points
Halcyon Abraham Ramirez I swear to go is it me or are the code challenges and the video lectures before each challenge are a quantum leap in difficulty?
Posted by Kenneth
"That said, this course is due for an overhaul and will probably get one this summer."
Please!!!! i would appreciate it if you did the quiz questions in the lecture and b r o k e t h e m d o w n doing it in work spaces and then having them do the same thing on the quiz. i wanted to honestly give up after dictionaries. but googled the answers so i can get the badge. but i honestly have been having a hard time knowing what your trying to quiz on cause the questions are so vague...
Emmet Lowry10,196 Points
Hi Chris could u break that down a bit more for me . Im finding it hard to get what its asking thanks
Emmet Lowry10,196 Points
Thanks for that wondering could u please explain some of the core concepts eg dictionarys and lists a little bit . I an finding python difficult and help on it to make it easier would be fantastic thanks.
random_items = 
for i in range(y):
Blake Handson1,642 Points
The question is rather ambiguous.It's not actually returning a list of 'n random' items, n is in fact a set value am I not correct? It is then choosing a 'random' item from that particular loop instance based on the passed in 'n' value. So I suppose technically, you are receiving a list of random items, however, the number of times you loop is not based on a random n value which is how I read the question. In saying that I am happy to admit I am completely mis-interpreting what was being asked in the question haha
Alexey Li8,936 Points
def nchoices(itr, int_1):
list_of_n = 
while int_1 > 0:
random_item = random.choice(itr)
int_1 -= 1