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 trialRabih Atallah
3,405 Pointsplease why my code return only 1 value instead of x values
Please how can I fix my script so it can return x values instead of 1. With the Print statement it returns x values, but with the Return statement it returns 1. I appreciate your help
import random
def nchoices(L,x): # L is an iterable, x is an integer
for i in range(x):
return random.choice(L)
1 Answer
Victor Rundbaken
14,037 PointsA return exits the function so as soon as the loop runs once it will exit. You would need to append each value to a list and then return the list outside of the loop.
Rabih Atallah
3,405 PointsRabih Atallah
3,405 PointsThanks so much Victor for this clarification it worked