Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
217,577 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.