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 trialConor McCracken
718 PointsPython Basics: Letter Game Challenge 1 I do not understand why my code doesn't work.
I cannot figure out what is wrong with my code. I am currently getting a Name Error saying my function's name is not defined. Help?
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
import random
def random_item(iterable):
random_index = random.randint(0, len(iterable) - 1)
return(index(random_index))
2 Answers
Domenic Carobine
9,014 PointsHey, I think the NameError is coming from "index" in your return statement. Also, you need brackets [] to index a string. If you switch up your return statement like I have below you should be able to pass the quiz:
import random
def random_item(iterable):
random_index = random.randint(0, len(iterable) - 1)
return iterable[random_index]
Conor McCracken
718 PointsThank you very much! It worked like a charm