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 trialianmckellar
4,344 PointsRandom 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
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
231,236 PointsIt 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ö
2,766 Pointsyou 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