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 trialMit Sengupta
13,823 PointsWhat is wrong with the code?
Here's my code :
import random
def random_item(iterable): iterable = random.randint(0, len(iterable) -1) return iterable
1 Answer
akak
29,445 PointsYou're returning number while the challenge wants a letter in the word that is fed to the function. Based on your code if you call it right now like this: random_item("example") you'll get random numbers like 3, 4, 1 etc. The goal is to get the letter associated with that number. I would refactor it like this:
def random_item(iter):
number = random.randint(0, len(iter) -1)
return iter[number]
# or shorter but less readable
def random_item(iter):
return iter[random.randint(0, len(iter) -1)]