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

Run offline to get correct, but not correct here

I have used an offline version to verify my code, but the code says here that it is not OK (I'm supposed to create a list of N random elements from an initially defined list):

import random # import random tooling for choice function

list = [1, 'Texas', 3, 'Girl', 'Boy', 6, 7] # define list n = 3 # define integer int(n)

def nchoices(list, n):

# initialize list & counter
count = 0
list_fin = []

while (count<n):
    count += 1 # interate variable up 1
    # use choice function to append a random number to list
    list_fin.append(random.choice(list)) 

# Return list_fin variable
return list_fin

list_fin = nchoices(list, n) print(list_fin)

Can anyone help me out?

choices.py
# The following function takes in an integer and list
# It then returns a list of N random choices where N is the integer number

import random # import random tooling for choice function

list = [1, 'Texas', 3, 'Girl', 'Boy', 6, 7] # define list
n = 3 # define integer

def nchoices(list, n):

    # initialize list & counter
    count = 0
    list_fin = []

    while (count<n):
        count += 1 #interate variable up 1
        list_fin.append(random.choice(list))

    # Return list_fin variable
    return list_fin

list_fin = nchoices(list, n)

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Your code runs beautifully! However, Treehouse doesn't know that. And the reason Treehouse doesn't know that is because they are going to run your code and send in their own list and integer. And you're overwriting it with your own declarations and call to the function. So if I remove that part, your code passes. Take a look... this is your code btw (just slightly modified):

import random # import random tooling for choice function


def nchoices(list, n):

    # initialize list & counter
    count = 0
    list_fin = []

    while (count<n):
        count += 1 #interate variable up 1
        list_fin.append(random.choice(list))

    # Return list_fin variable
    return list_fin

Hope this helps! :smiley:

Thanks Jennifer! That worked great :)