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 trialoliverchou
20,886 PointsI couldn't get this right!
Is there anything wrong in my code? I've paused learning python for a while and I just couldn't fix the error. Anyone can help?
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
import random
def random_item(x):
return (x[random.randint(0, len[x]-1)])
3 Answers
Thomas Fildes
22,687 PointsHi Oliver,
You have nearly got it right the only thing that you need to do is change the parenthesis and brackets. Here is following code below if unsure:
import random
def random_item(x):
return x [random.randint(0, len(x)-1) ]
Shahar Rosen
18,957 PointsWhen calling "len" function on x you use square brackets - len[x]. use len(x) instead...
oliverchou
20,886 Pointsgot it
oliverchou
20,886 Pointsoliverchou
20,886 Pointsbut why is there a space between the "x" and the "[]" on the 4th line?