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
170,315 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
170,315 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!