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 trialAlexander Hansson
2,083 PointsIs there any better way than this?
I see that my code works but I failed the assignment. Could someone please provide with other possible examples for completing this task with some explanations? Thank you.
import random
def nchoices(iterable, integer):
rand_list = []
while integer:
rand_list.append(random.choice(iterable))
integer -= 1
return(rand_list)
nchoices("hej",3)
2 Answers
Vittorio Somaschini
33,371 PointsHello Alexandre.
I think that is pretty neat.
Maybe this could be a good solution too?
import random
def nchoices(iterable, integer):
new_list = []
for number in range(0, integer):
new_list.append(random.choice(iterable))
return new_list
Vitto
Steven Parker
231,275 PointsYou don't need to call your function, the challenge does that for you.
But otherwise, you code solves the challenge. I tried cut-and-paste directly into the challenge and it passed.
Good job!
Alexander Hansson
2,083 PointsOh, thanks. =) But are there any other possible neat solutions that you could explain? That would be awesome since I tried to improve the code but didn't manage come up with an alternative solution.