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 trialMichele Dashiell
1,564 PointsWhy couldn't I do it like this?
So I have looked at the other posts on this challenge and I have done it slightly differently. Another line of code I know-- but why wouldn't this also work? Thanks in advance.
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
import random
def random_item(ite):
member= [random.randint(0,len(ite)-1)]
return member
2 Answers
Jennifer Nordell
Treehouse TeacherYour code would only return the random number generated. Not the letter at the index of the random number. Let's say we rolled a 4. Your code would return the 4. The passing code would return a "h". Because "h" is at the 4th index in "Treehouse" (assuming that's the string they sent in).
Michele Dashiell
1,564 PointsAhh thank you! I misread what the output was supposed to be.