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

What am I missing on this nchoices() func? I'm pretty sure this code is good - unless I'm misunderstanding the challenge

def nchoices(iterable,integer):
  my_list = []
  x = 0
  while x < integer:
    my_list.append(random.choice(iterable))
    x += 1
  return my_list
Ricky Catron
Ricky Catron
13,023 Points

Your solution looks fine to me it is possible that the question/site is having problems.

Agreed...I think there may be issues...thanks Ricky

Ricky Catron
Ricky Catron
13,023 Points

Of course. I tested your solution and then one of my own and both threw the same error.

1 Answer

Hi Kurt,

You have to import the random module in order to use random.choices()

As far as I know, the code challenges don't import modules for you.

Ricky Catron
Ricky Catron
13,023 Points

Face meets desk should have seen that.

Well, I think the error message throws you off.

Ricky Catron
Ricky Catron
13,023 Points

It does, why are the error messages so vague? Any unix console would have returned a far better error.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

That's because, like a dummy, I only assumed the NameError would come from the student not creating nchoices. I'll update it.

Thanks Jason Anello . Good catch

You're welcome, Kurt.

Thanks Kenneth. The new error message should lead people in the right direction.