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 trialAarron Walter
2,217 PointsRandom choices and looping
I'm stumped on this challenge. I need to build a list of random choices, but the number of choices is determined by an int passed into my function. Looping on that int the the part that is tripping me up.
2 Answers
Chris Freeman
Treehouse Moderator 68,457 PointsHere is the flow:
# remember to import random
import random
def nchoices(iter1, count):
results = []
# run loop 'count' times
for _ in range(count):
# append new random item from iter1 to results list
results.append(random.choice(iter1))
return results
Note that since I don't need the value of the loop variable, I used "_
" as a "throw-away variable".
Aarron Walter
2,217 PointsThanks, Chris! Looping on a range was exactly the answer I needed. I appreciate your help.