Python Python Basics (2015) Letter Game App Random Item

Doyle Lardell
PRO
Doyle Lardell
Pro Student 888 Points

example isnt good enough to help. Trying to get answer for some time now without enough clues as to what I'm doing wrong

example isnt good enough to help. Trying to get answer for some time now without enough clues as to what I'm doing wrong

item.py
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
import random
x = "Treehouse"
def random_item(x):
    b = random.randint(0, (len(x)-1));
    for b in x:
    return b

1 Answer

Jennifer Nordell
STAFF
Jennifer Nordell
Treehouse Staff

Hi there! You're almost there. In fact, I think you might have overthought this a bit.

First, you do not need to loop through the iterable. Let's say that I have a string "hello". If I wanted to get the "o" from that string I could do it like this:

my_string = "hello"
my_o = my_string[4]

This challenge is much the same. You're generating the random index just fine. Now all you need to do is return the part that is at that random index.

So the line you're missing:

return x[b]

This will return the part of that iterable that resides in the random index you generated.

Hope this helps! :sparkles: