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 trialtyler bucasas
2,453 Pointsrandom_item
been stuck on this for awhile now, am i using the functions correctly? please help
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
behar
10,799 PointsHey 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
2,453 Pointstyler bucasas
2,453 Pointsthank you, i really appreciate the help!