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 trialJamie Evans
2,092 Pointsnchoices, I passed but not happy with my answer, is there a better way?
Hello all
I have passed the challenge, but i am not happy that this is a good way of doing it. if someone could please show me a better more efficient way of doing it. Don't think its very python like.
import random
def nchoices(lst1, int1):
count = 0
new_list = []
for item in lst1:
if count < 5:
new_list.append(random.choice(item))
count+=1
else:
break
return new_list
2 Answers
Michael Parthum
1,468 PointsBelow is what I passed with. This is as simple as I was able to break it down to.
import random
def nchoices(supplied_list, n):
count = 0
random_list = []
while count < n:
random_list.append(random.choice(supplied_list))
count += 1
return random_list
anhedonicblonde
3,133 PointsAwesome, that makes sense to me! thanks much for posting your code! :)
Mike Tribe
3,827 PointsSame
import random
def nchoices(itr, intgr):
randList = []
count = 0
while count < intgr:
randList.append(random.choice(itr))
count += 1
return randList
Jamie Evans
2,092 PointsTurns out I completely got the wrong end of the what the challenge was meant to be. It was supposed to give the random.choice for the number in the integer:)
import random
def nchoices(lst1, int1):
count = 0
new_list = []
for item in lst1:
if count < int1:
new_list.append(random.choice(item))
count+=1
else:
break
return new_list
anhedonicblonde
3,133 PointsI'm completely lost with this one. Can someone explain this to me? I had no idea how to do this challenge and I still don't really get it. Thanks in advance.
Jamie Evans
2,092 PointsJamie Evans
2,092 PointsI tried doing while count < 5: instead of if, but "code took too long to run" for some reason:)