Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (2015) Letter Game App Random Item


Stuck on this one. I have tried 3 different (VERY SIMILAR) variations of the attached code/screenshot and am not getting it solved. I am thinking the problem is that the 'return'?

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

1 Answer

Steven Parker
Steven Parker
215,939 Points

Yes, partly. You want to return an element from the iterable. But to do that, when you call randint (the first time) you will need to save the value it returns in a variable. Then you can use that variable as an index to select an item from the iterable.

I'm lost. don't know what you just said, but I tried this and it did not work.

import random def random_item(num): iterable = random.randint(0, len(num - 1)) random.randint(0, len(num - 1)) return(iterable)

Steven Parker
Steven Parker
215,939 Points

It's hard to understand that unformatted, but it looks like you're calling random.randint twice instead of just once. But it does look like you're saving the first call in a variable named "iterable". Instead of returning that directly, you should use it as an index value for the argument ("num[iterable]").

And when posting code, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.