Welcome to the Treehouse Community
Looking to learn something new?
Not Working. Why?
what is the issue with the code
# EXAMPLE # random_item("Treehouse") # The randomly selected number is 4. # The return value would be "h" import random def random_item(box): x = random.randint(0,len(box)-1) return box.index(x)
The problem is your use of the
index is used to find the index of a particular value in a list. It is not used to retrieve a value. To retrieve the value you can simply use bracket notation like this:
import random def random_item(box): x = random.randint(0,len(box)-1) return box[x]
index() method in Python is designed to return the first index at which a member is found. This means that, in the way you're using it, you are assigning a random number to
x which is then being looked for in the string. This will result in a TypeError, since you cannot implicitly convert an
int to a
str. Though, ignoring that, if you were to cast the
x to string by using
str(x) instead, it's worth mentioning that you'll usually receive a ValueError, as you will not find the substring of a random number
x within a string "
Treehouse" or similar strings. In your case, what the Challenge is actually asking for is the member value, which means you should reference your iterable
box and pass in the known index
x as it's parameter (i.e.