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

Random Task Can't Pass

below is my code, and the system shown:"Bummer! Didn't get a correct option back from random_item"

I ran my codes works well. I don't get the problem

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

word = 'apple'  
def random_item(word):
    length = len(word) - 1
    num = random.randint(0, length)
    word[num]    
random_item(word)

3 Answers

Stuart Wright
Stuart Wright
41,118 Points

You missed that all-import return keyword at the end of your function. Your function currently doesn't return anything.

import random

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

Hi Stuart, Thanks for your reply, how about I did as below:

import random

word = 'apple'  
def random_item(word):
    length = len(word) - 1
    num = random.randint(0, length)
    w = word[num]
    print(w)   
random_item(word)
Stuart Wright
Stuart Wright
41,118 Points

That doesn't work because you print w instead of return it. If you replace your print(w) line with return w then it works perfectly.

Thank you, guys for all the inspiration. I looked at old notes and use index! Here is my code:

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