Python Python Basics (2015) Letter Game App Random Item

Cant understand why this code is not working?

EXAMPLE

random_item("Treehouse")

The randomly selected number is 4.

The return value would be "h"

import random def random_item("iterable") random.randint(0, (len(iterable)-1)) return index.randint()

item.py
# EXAMPLE
import random 

def random_item("iterable"):
    random.randint(0, len(iterable) - 1)
    return index.randint()
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"

1 Answer

Cameron Nilon
Cameron Nilon
15,487 Points

Hey Omari,

Let's break it down a bit. Remember to index an iterable you can use '[]' in other words, say you have "hello" and you want to get the letter 'e', you would get this by indexing with square brackets [1], remember it is zero indexing. Now to use randint() you basically have that correct, except iterable is not defined. randint() returns a number between your first arg and your second arg. Also, I wanted to point out that your parameter is syntactically incorrect, lose the quotations or assign "iterable" to word without quotations ie iterable = "iterable".

Allow me to give you an example on how you can do this challenge.

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