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

FHATUWANI Dondry MUVHANGO
FHATUWANI Dondry MUVHANGO
17,796 Points

im having trouble with random.randit

how do i create random.choice

item.py
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
import random
def random_item(words):
    random_item = random.randit(0, len(words))
    return words

3 Answers

Steven Parker
Steven Parker
231,275 Points

:point_right: You may just have a typo.

It looks like you wrote randint with only one "n".

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,702 Points

Oops, should be randint not randit

Also you will only want to return 1 of the characters from the entire word.

returning words will just return the thing you passed in.

HINT

random_item = random.randit(0, len(words)) This however is holding a number(integer) that you can use as an index to grab a character out of words.

FHATUWANI Dondry MUVHANGO
FHATUWANI Dondry MUVHANGO
17,796 Points

Hey Chris. I tried using your hint, it doesn't seem to be working

Chris Howell
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Chris Howell
Python Web Development Techdegree Graduate 49,702 Points

You may have to re-read the prompt again because there is a portion I didn't give because I didn't want to give the whole answer.

Example

using random.randint(0, 10) will grab a number between 0 and 10. If you count the range of those numbers it would be 11 numbers total right? Counting 0 as the first then so on. So if the *len(words) was 10. We would be grabbing random items from a list with a range that was 1 more than the size we wanted.

An important part to take note in the challenge prompt is this part:

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.

Notice the minus one :)

Let me know if this helps, if not Ill give you a code example. Again im trying not to give you the exact answer because that takes out the challenge and takes away from your learning.