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

Mint Milano
Mint Milano
3,114 Points

i could not get it right! can i get a brief hint regarding the problem

could not find the error

choices.py
import random
def nchoices(itr,n):
    count=0
    random_list=[]
    while n>=count:

        temp=random.choice(itr)

        random_list.append(temp)
        count += 1
        return random_list

3 Answers

I agree with Steve and I would add that you should manage the readability a bit better. It's good to keep your code neat and tidy while learning so it becomes second nature later on.

import random

def nchoices( itr, n):
      count = 0
      random_list = []

      while n > count:
            temp=random.choice(itr)
            random_list.append(temp)
            count += 1

      return random_list
Steven Parker
Steven Parker
231,275 Points

:point_right: I see two issues:

  • The return statement should come after the loop (it's currently indented to place it inside the loop)
  • The while limit should be n > count (using n >= count creates one too many results)
Mint Milano
Mint Milano
3,114 Points

[solved] thanks again for helping me with the concepts

Mint Milano
Mint Milano
3,114 Points

[solved]Thanks for the extra info sir.