Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Conor 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