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

Question on the Random Choices Objective

I keep getting the error "Bummer! Expected 5 items, got 10." I have tried sending empty variables, declaring them outside the function, and everything else I can think of. When I run this script I get a something like "['a', 'b', 'c', 'a', 'c']" as my solution.

What is going wrong? I have had this problem before where the challenge program was sending things in for me, but I can never figure out what to change to get it to work correctly. Is there any way I can get the Challenges to show me debugging information?

choices.py
#Create a function named nchoices() that takes an iterable and an integer. 
#The function should return a list of n random items from the iterable where n is the integer. Duplicates are allowed.
import random
list = []
def nchoices(str,n):
    for x in range(n):
        list.append(random.choice(str))
    return list

print(nchoices(('a','b','c'),5))

EDIT: I solved it. Should have realized it was running the function for me, but my question about a debugger still stands

import random
def nchoices(str,n):
    list = []
    for x in range(n):
        list.append(random.choice(str))
    return list

1 Answer

Steven Parker
Steven Parker
229,582 Points

Good job on resolving your issue! :+1:

I don't think you can debug from within the challenge, but you can always copy the challenge code over to the workspace, and experiment with it there.