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 trialDoyle Lardell
Courses Plus Student 888 Pointsexample isnt good enough to help. Trying to get answer for some time now without enough clues as to what I'm doing wrong
example isnt good enough to help. Trying to get answer for some time now without enough clues as to what I'm doing wrong
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
import random
x = "Treehouse"
def random_item(x):
b = random.randint(0, (len(x)-1));
for b in x:
return b
1 Answer
Jennifer Nordell
Treehouse TeacherHi there! You're almost there. In fact, I think you might have overthought this a bit.
First, you do not need to loop through the iterable. Let's say that I have a string "hello". If I wanted to get the "o" from that string I could do it like this:
my_string = "hello"
my_o = my_string[4]
This challenge is much the same. You're generating the random index just fine. Now all you need to do is return the part that is at that random index.
So the line you're missing:
return x[b]
This will return the part of that iterable that resides in the random index you generated.
Hope this helps!