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

George Lugo
seal-mask
.a{fill-rule:evenodd;}techdegree
George Lugo
Python Web Development Techdegree Student 922 Points

hello i would like to see what i am doing wrong for the function above

def random_item("Treehouse"): random.randint(0, len("Treehouse" - 1) return("Treehouse")

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

def random_item("Treehouse"):
    random.randint(0, len("Treehouse" - 1)
    return("Treehouse")

1 Answer

First of all, you forgot to import random. To import random use import random. Secondly, your argument for your function is a string (words surrounded in either apostrophes or quote marks). Third, when you tried to find the length of the argument, you subtracted 1 in the len function, you can't subtract an integer from a string. Lastly, when you went to return the answer, you returned the string without any sort of square brackets at the end to specify a location in the string. A completed challenge is here:

import random

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