Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Collections (Retired) Dungeon Game Random Choices

Kang-Kyu Lee
Kang-Kyu Lee
52,045 Points

my answer doesn't pass..

import random

def nchoices(an_iterable, choose_num):
  list_of_items = []
  for _ in range(choose_num):
    pop_item = an_iterable.pop(random.randrange(len(an_iterable)))
    list_of_items.append(pop_item)
  return list_of_items  

iterable = [1,2,3,4,5,6,7,8,9]  
integer = 4  
nchoices(iterable,integer)

Maybe I did it wrong, only I couldn't figure what it is... it works at workspace

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

It's not that there's anything that says to allow duplicates, nothing says to not allow duplicates, so you shouldn't assume that.

Kang-Kyu Lee
Kang-Kyu Lee
52,045 Points

Thank you folks. It's given the function nchoices() takes the number of items to return, I assumed the duplicates might not be counted, and I tried the same one already picked not be an option. However, because not given as it said, then both possible answers should be working?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

To me, eliminating duplicates would require the explicit instruction to do so. I'll add a note to the instructions, though, to prevent future confusion.

Hi kang kyu lee,

I don't think the challenge is specific enough on this but I think it wants the solution to allow duplicates. Whereas, you're popping off items and shortening the iist and making sure the same one isn't picked twice.

I changed your code so that instead of popping off the item at that random index you access the item at that index and it passed the challenge.

I have not been through this course so I don't know if there is something in the videos that would have hinted that you should allow duplicates here.