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

Gary Burner
Gary Burner
10,167 Points

Not sure what Im doing wrong. Getting an error that the function is not defined.

Can't think of anything else I could change to avoid that error - any help would be appreciated.

item.py
import random

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


# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"

1 Answer

Ryan S
Ryan S
27,276 Points

Hi Gary,

You have to be careful with variable names. You've imported the random module but then used a variable with the same name and are attempting to access random.randint() in its definition. So you are effectively overwriting the random module.

It is important to choose variable names that are not module names, keywords, or other reserved Python words. Other than that your logic looks good and should pass if you change the variable name.

Hope this helps.