Python Python Basics (2015) Letter Game App Random Item

tyler bucasas
tyler bucasas
2,453 Points

random_item

been stuck on this for awhile now, am i using the functions correctly? please help

item.py
import random

def random_item(word):
    num = random.randint(0,len(word)-1)
    return num.index(word)


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

1 Answer

Hey tyler! Your close! The only issue is how you sellect indexes. Sellecting indexes of strings or list is done like this:

myList = [1, 2, 3, 4, 5]
myList[1] # Would return 2, because the item at index 1 is 2
myString = "test"
myString[2] # Would return "s", because the item at index 2 is "s"

So instead of saying:

return num.index(word)
# Say:
return word[num]

Hope this helps!

tyler bucasas
tyler bucasas
2,453 Points

thank you, i really appreciate the help!