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

Alex DiFazio
Alex DiFazio
579 Points

Not understanding code challenge

I'm beyond baffled by the instructions in this code challenge, and I'm unsure where to turn for an explanation of how to complete what it's asking. The challenge loses me completely with what it's asking to return, but as a whole I'm not grasping the concept or goal of this challenge.

Any help is appreciated. I've attached my "code" for reference, but I highly doubt it will be of use, being that I don't even know what my target is.

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

import random

def random_item(argument):
    number = random.randint(0, 6)
    return argument.index('number')

1 Answer

Steven Parker
Steven Parker
231,236 Points

Basically, if you give this function a list, you get back one item from it. And if you give it a string, you get back one character from it. In either case, the one it gives back should be picked randomly.

The example shows giving it "Treehouse" and getting back "h", but it could also have given back "s" or "u" or any of the other letters.

Does that make more sense?

Alex DiFazio
Alex DiFazio
579 Points

Okay, that's helpful, but there's still a disconnect on where exactly I'm supposed to be pulling these factors from. Then most of which- where does the index come in?

I like that these challenges often introduce something that wasn't specifically covered in the past video or handful of videos, but this one I am just having a really hard time wrapping my head around.

Steven Parker
Steven Parker
231,236 Points

The index is a random number, picked from a range that spans the number of items or letters in the original iterable. The code shown above calls the "randint" function to do this, but it should use the length of the iterable as the high value of the range instead the fixed value "6".

Then, it's just a matter of returning the item at that index, which is done by using the bracket notation for indexing. The "index" method is a searching function that is not needed for this challenge.

Alex DiFazio
Alex DiFazio
579 Points

Thank you for your help on this. It took forever, but I was able to complete the challenge thanks to your insight!