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 trialPetko Delchev
Courses Plus Student 2,103 PointsI'm stuck in the random..
I know I'm close, but I can't do it..
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
import random
def random_item(arg):
random_arg = random.randit(0, len(arg)-1)
return arg.index(random_arg)
2 Answers
Chris Freeman
Treehouse Moderator 68,441 PointsYou have right approach. Three errors need fixing.
- typo should be
random.randint
-
return
statement is indented too far. It should align with statement above it. - to reference the index of a container object use square brackets (also called braces):
arg[random_arg]
Post back if you need more help. Good luck!!!
Petko Delchev
Courses Plus Student 2,103 PointsDone!
Thank you again!
import random
def random_item(arg):
random_arg = random.randint(0, len(arg)-1)
return arg[random_arg]
Petko Delchev
Courses Plus Student 2,103 PointsPetko Delchev
Courses Plus Student 2,103 PointsHi Chris,
Thank you for your time.
I followed you instructions and I received this error:
Bummer! TypeError: 'builtin_function_or_method' object is not subscriptable
This is saying I haven't imported the random?!
Chris Freeman
Treehouse Moderator 68,441 PointsChris Freeman
Treehouse Moderator 68,441 PointsRemove the code ".index". Look again at the third hint.