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

Kade Carlson
Kade Carlson
5,928 Points

Not sure what to return

Not sure how I should code this

item.py
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
import random
def random_item('apple'):
    random.randint(0, len('apple') - 1)
    return 

1 Answer

Thomas Harris
Thomas Harris
7,647 Points

Hello! When your random_item() function takes an argument, that's the variable to use and you have to pass a string to it. Then your return value should be the variable passed to it, but only the one letter referenced by the random index "i"

Should look like this:

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

print(random_item('apple'))