Python Python Basics (2015) Letter Game App Random Item

Petko Delchev
Petko Delchev
Python Web Development Techdegree Student 1,912 Points

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 55,677 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!!!

Petko Delchev
Petko Delchev
Python Web Development Techdegree Student 1,912 Points

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 55,677 Points

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

Petko Delchev
Petko Delchev
Python Web Development Techdegree Student 1,912 Points

Done!

Thank you again!

import random

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