## 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.

###### Joe Simonsen

2,684 Points# Pick a number challenge

Stuck here. Here is my code thus far:

```
import random
minus_one = []
def random_member():
x = len(minus_one)
num = random.randint(0,x)
return
```

## 4 Answers

###### Stone Preston

42,016 Pointsit looks like you misunderstood the instructions a bit.

it says **Now, make a function named random_member that takes a list. It should generate a random number between zero and the length of the list (minus one), and return the list item at that index.**.

So your function needs to take a list argument (currently yours takes no arguments). it needs to generate a random number between zero and the length of the list passed in minus one, (the list isnt called minus one though, this is where you misunderstood). then return the item in the list at the index of the random number

```
import random
def random_member(some_list):
# x is the upper bound of the random function. it needs to equal the length of the list - 1 so we dont go out of index in the list
x = len(some_list) - 1
#num is the random number generated by the random function. it will be between 0 and the length of the list argument passed in - 1
num = random.randint(0,x)
# returns the list item at the random index
return some_list[num]
```

###### Kenneth Love

Treehouse Guest TeacherLet's say we have a list:

```
a_list = [1, 2, 3, 4, 5]
```

If I get the `len()`

of that list, I'll get 5. So I feed that to `randint`

.

```
num = randint(0, len(a_list)) # the len will be 5
```

Now Python gives me back a 5, for example. So we try to get that index from our list.

```
a_list[num] # which we're pretending is 5
```

Well, `a_list`

only has indexes from 0 through 4, there is no 5 index. So everything blows up and we have to run screaming from the room until the fire burns itself out.

Or we just subtract one from the length :)

###### Jan M

1,053 PointsWhat is the (for?

###### Kenneth Love

Treehouse Guest TeacherJan M not sure what you're asking about.

###### MUZ140734 Tinashe Chibwe

16,960 Pointsafter a while of trial and error i finally cracked it...

import random

def random_num(num): return random.randint(1, num)

# remember we already have 1, so I used (num) as my argument. then 'return'

###### Ann Cascarano

17,191 PointsThe random number that you are going to generate using the randint method will be used as an index to extract a given number from your list. You never know what index you are going to get - and thus - you don't know which number from the list it will pull out.

Now - because we know that indexes are 0-based - you need your pool of numbers from which your random generator will draw from to include 0, 1, 2, 3 and so on until all the elements in the list have been accounted for. When you are using the len method on a list, for example, you are counting the number of elements in the list - and here is the tricky part: A list of 4 items will have the indexes 0, 1, 2, 3. Therefore - in order to use your randint method to produce a valid random index for your list, you have to remove 1 from the length of the list.

your_list = [1, 2, 3, 4, 5]

random_num = randint (0, (len (your_list) - 1))

your_list[random_num]

If you omit the '-1', your random_num will eventually give you a '5' - and since there is no 5th index in the list, the program will crash.

Hope this helps!

###### Adrian lai

1,147 PointsHi there, quick question regarding this challenge.

Why do we use:

return some_list[num]

rather than just: return some_list

###### Stone Preston

42,016 Pointsbecause that would return the whole list. you want to return a random **member** of the list, not the list itself

```
return some_list[num]
```

returns the member of some_list at index num, which is the random index you generate.

## khoreysmith

3,524 Points## khoreysmith

3,524 PointsThe following code worked for me:

***Only problem is I don't know why this works eventhough I didn't follow the minus 1 part of the instructions.