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

Alexandra Montgomery
Alexandra Montgomery
520 Points

Isn't this right?

You've seen how random.choice() works. It gets a random member from an iterable (like a list or a string). I want you to try and reproduce it yourself. First, import the random library. Then create a function named random_item that takes a single argument, an iterable. Then use random.randint() to get a random number between 0 and the length of the iterable, minus one. Return the iterable member that's at your random number's index. Check the file for an example.

input random

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

1 Answer

Jason Anders
Jason Anders
Treehouse Moderator 145,692 Points

Hey Alexandra. Welcome to Treehouse.

It is correct except for one thing. You want to import random ... not input. The rest of the code is correct, so just change that one thing and you're good to go.

Keep Coding! :)