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

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 :)