Python Python Basics (2015) Letter Game App Random Item

Canen McCaslin
Canen McCaslin
552 Points

Bummer! Try again!

I have rewritten this multiple ways but nothing works.
import random

    def random_item(iterable):
        number = random.randint(index(iterable)
        final = number -1 
        return final
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"

1 Answer

Logan R
Logan R
22,911 Points

Hey there!

So you are super close! Let's start by looking at how to use the "random.randint()" function. The documentation says:

random.randint(a, b)
# Return a random integer N such that a <= N <= b.

This means that we need to pass it 2 integers, a and b, and it will pick a number between them.

For example:

>>> random.randint(5, 10)
>>> random.randint(-3, 7)

So getting back to the challenge's problem statement: "Then use random.randint() to get a random number between 0 and the length of the iterable, minus one.". We want a range between "0" and "length of iterable". That means that your "a" input will be "0" and your "b" input will be the length of your iterable array.

The final issue that you have is that you need to return the item, not the index of the item. Hint: return item[index].

Hopefully this helps solve your issue! If not or something is still not clear, feel free to reply to the comment!