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 trial

Python Python Collections (Retired) Dungeon Game Random Choices

Rabih Atallah
Rabih Atallah
3,405 Points

please 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

choices.py
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

A 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
Rabih Atallah
3,405 Points

Thanks so much Victor for this clarification it worked