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 trialPat Dudar
2,490 Pointsitem.py random_item()
Hi, guys! I'm trying to figure out how to define the length of my iterable - 1, and am wondering if len(I) would be the correct syntax for random.randomint(). I don't think it is, but I think I'm on the right track. Thank you for your help
Pat
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
import random
def random_item(i):
i = random.randint(0, len(i-=1))
return(iterable)
1 Answer
Stuart McIntosh
Python Web Development Techdegree Graduate 22,874 PointsHi there, yes you are on the right track, however just a small change. I also would not use 'i' . Random.randint will return an integer. So to get the result you will have to return that index of the passed in list i.
num = random.randint(0, len(i)-1)
Hope this makes sense