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 trialVikshith Vishwanath
Courses Plus Student 820 PointsBummer! Didn't get a correct option back from 'random_item'
import random
def random_item(iterable): num = iterable[random.randint(0,len(iterable)-1))] return(num)
actually this code works and the one without the iterable in the line 4 (just like the one attached ) doesn't work. why is it doesn't work?
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
import random
def random_item(iterable):
num = random.randint(0,len(iterable)-1)
return(num)
1 Answer
Jennifer Nordell
Treehouse TeacherHi there! They're correct. You're not returning the correct thing. You randomly generate a number and then you're returning that number. What they want is for you to return the letter at that index.
So let's say you randomly generated a 2. It would want you to return the thing at index 2 of the iterable passed in. In this example, it would be iterable[2]
.
Hope this hint helps!