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 trial

Python Python Basics (2015) Letter Game App Random Item

oliverchou
oliverchou
20,886 Points

I 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?

item.py
# 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
Thomas Fildes
22,687 Points

Hi 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) ]
oliverchou
oliverchou
20,886 Points

but why is there a space between the "x" and the "[]" on the 4th line?

Shahar Rosen
Shahar Rosen
18,957 Points

When calling "len" function on x you use square brackets - len[x]. use len(x) instead...