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

Random Choices help

Hey everyone,

I'm stuck on a code challenge.. The error message Treehouse is giving me is that nchoices() isn't defined. Which confuses me. But really what I'm confused about is making the length n, I understand the concept of n just not how to apply it.

Thanks in advance

choices.py
import random

def nchoice(iterable, interger):
    my_list = []
    for item, number in enumerate(iterable):
        random_item = random.choice(iterable, interger)
        my_list.append(random_item)
    return list

2 Answers

Steven Parker
Steven Parker
218,861 Points

:point_right: It looks like you have mutliple issues here:

  • The challenge asked for nchoices (with an "s"), but you defined nchoice
  • the integer argument specifies how many (n) choices to return, so use it in a range for your loop
  • you won't need enumerate
  • random.choice needs only the iterable as its argument
  • the return should pass back the constructed list (my_list)

I'll bet you can fix it now without an explicit spoiler.

Florian Benkö
Florian Benkö
2,766 Points

you did some good work. if i can give you some hints: the for loop you wont use, because you really want to repeat the value of the integer. like integer = 5 -> you want to run it 5 times depending on nothing else. better use other loop ;)

also you want to do the random.choice function only on the iterable, which you want to split first with split()

i am a beginner too i hope its a bit helpful