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 trialCanen McCaslin
552 PointsBummer! 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
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
1 Answer
Logan R
22,989 PointsHey 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)
6
>>> random.randint(-3, 7)
-1
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!