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 Basics (2015) Letter Game App Random Item

I was able to get this far, am I close?

Please don't give me the full answer, just tell me what I'm doing theoretically wrong :)

Any help is appreciated.

item.py
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
import random

def random_item(iterable):
    return random.randint(0, len(iterable)) - 1 

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

You are close. You've generated the required random number. Now use that number as the index to select an item from the iterable and return that item.

Also, you should probably put the "-1" inside the randint argument with the len(iterable) instead of subtracting 1 after generation the number. Otherwise, if the randint was 0, you'd be left with a negative index (which would work but not for the reasons you want).

Post back if you need more help. Good luck!!

I did this. I'm kind of confused what I'm doing wrong haha. Do you know my issue?

import random

def random_item(iterable):
    random.randint(0, len(iterable - 1))
    return iterable.index(1)
David Evans
David Evans
10,490 Points

Hi Charles,

Rather than use the .index() try thinking of it this way.

Iterable is an array, so how would call a specific index of an array?

import random

def random_item(iterable):
    random.randint(0, len(iterable) - 1) #I moved the - 1, but do you think you should assign this to a variable so you can use it?
    return iterable.index(1) #How would you call a specific index of the iterable array here?
Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

You've moved the -1 one paren too far. It should be outside the Len function.

Assign the result of the randint to a variable, such as index and use that variable as the index of the iterable in the form iterable[index].

Got it guys! Thank you so so much for the help, learnt heaps!

This was my final answer:

import random

def random_item(iterable):
    num = random.randint(0, len(iterable) - 1)
    return iterable[num]
David Evans
David Evans
10,490 Points

Hi Charles,

So to not give the answer, I'll just draw your attention to the last line of the instructions.

Return the iterable member that's at your random number's index.

What you have done so far has gotten the index as random.randint(x,y) returns an integer. ( You will need to adjust your minus 1, its not quite in the correct place. )

You'll need to subtract one from the iterable, and not the random number you generate.

Once you fix the subtraction from the length of the iterable not the random number generated, keep in mind that you just generated an integer which will be your index and then you'll need to find the 'iterable member' or the single character that is associated with the number you generated.