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

Erin Leathers
Erin Leathers
3,562 Points

Stage 1 of 1 code challenge Dungeon game.

Create a function that returns a list of random items from an iterable. The function, named nchoices(), should take an iterable and an integer for the number of items to return. Duplicates are allowed.

Haha I searched for a little help on the forums and no one has posted. Lucky me now I am the first one to have trouble at this stage !

I get everything Kenneth had in his video but I always get stuck on creating these functions. Specifically "The function, named nchoices(), should take an iterable and an integer for the number of items to return"

SO my function should take a list, tuple, or something that is iterable , I am confused on how to make this ,

Can someone give me a hint ?

Thanks

6 Answers

def function123():

example of function

squares = random.choice()

example of random iterable

Does this help? =)

Erin Leathers
Erin Leathers
3,562 Points

Hmmm not really .

Have to make a function that accepts an iterable and a int: def nchoices(ran_list, ran_int):

Now I have to return as many random numbers from a list to whatever the int is ?

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

You got it. You don't know, or need to know, what the iterable is. But now that you have an iterable, and a number of items we want from the iterable, you need to pull that many items out of it and return them all.

Erin Leathers
Erin Leathers
3,562 Points

Sorry still having trouble, I know its something easy but need a little more of a nudge. If I don't put in the int I can get it.

So basically I have to do this as many times as the int that is passed but need some help.

random_list = list(range(51))

import random

def nchoices(ran_list):
    num = random.choice(ran_list)
    return num
Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Let's write this out in comments.

import random  # well, yeah, we need this to get random things


def nchoices(iterable, num):
    # Need a variable to hold our randomly selected choices
    # Now we need a for loop that goes up to `num`
        # Pick a random member out of `iterable
        # Put the random number into our choices variable
    # Return our choices variable
random_list = list(range(51))
# need empty list
# try random_list = []
# this should be inside the function

import random
# good import

def nchoices(ran_list):
# needs to take two items such as iterable, num


# should have while statement under list
# example: while len(random_list) < num:

    num = random.choice(ran_list)
# should be different variable than what will be added to iterable
# example:  item = random.choice(iterable)

 # need to use list.append() and pass inside what will be added 

    return num
    # should return random_list

Did I give best answer? =D

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

If you gave the best answer, they'll award you with it. No need to ask for it every time.

Erin Leathers
Erin Leathers
3,562 Points

Thanks Kenneth , I have not been home to plug this in yet and try it. I think I am not comfortable with for loops and that is one of my problems.

I will plug it in and let you know.

Thanks

Erin Leathers
Erin Leathers
3,562 Points

I got it guys , thanks for the help.

Kenneth it worries me that it took me so long to compete this challenge and needed so much help when it was in the end very simple : ) .

I guess I will keep on immersing myself and hope more sinks in.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Don't worry! It just means you need more practice on this kind of problem. That's good! It's great to know what you need to watch out for.

Take your time, practice on your own, and you'll get it all figured out.