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

I'm stuck in the random..

I know I'm close, but I can't do it..

item.py
# 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
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

You 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!!!

Hi 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?!

import random

def random_item(arg):
    random_arg = random.randint(0, len(arg)-1)
    return arg.index[random_arg]
Chris Freeman
Chris Freeman
Treehouse Moderator 68,454 Points

Remove the code ".index". Look again at the third hint.

Done!

Thank you again!

import random

def random_item(arg):
    random_arg = random.randint(0, len(arg)-1)
    return arg[random_arg]