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 trialRyan Felton
1,471 Pointsnot sure what to do
help
import random
def random_item(sample):
random_item=random.randint(0,len(sample)-1)
return sample
1 Answer
Jeffrey James
2,636 PointsYou're returning the function argument:
import random
def random_item(sample):
random_item=random.randint(0,len(sample)-1)
return random_item
Try something like returning "random_item", which is the variable you likely intended to return.
>>> import random
>>> def random_item(sample):
... random_item=random.randint(0,len(sample)-1)
... return random_item
...
>>> random_item([1,3,44,90])
3
>>> [random_item([1,3,44,90]) for x in range(100)]
[0, 2, 0, 2, 3, 1, 2, 0, 3, 1, 2, 0, 3, 2, 3, 2, 0, 1, 0, 1, 3, 0, 1, 0, 3, 3, 3, 1, 2, 1, 1, 2, 2, 0, 3, 0, 0, 3, 1, 0, 3, 3, 1, 3, 3, 0, 3, 3, 0, 1, 0, 3, 2, 2, 1, 1, 3, 1, 2, 3, 2, 3, 0, 3, 1, 0, 2, 2, 0, 2, 0, 3, 1, 0, 3, 1, 3, 3, 3, 0, 3, 0, 2, 0, 1, 1, 1, 3, 3, 0, 2, 3, 1, 0, 3, 3, 0, 0, 0, 2]
Jeffrey James
2,636 PointsJeffrey James
2,636 PointsAlso, based on the func you wrote, I would imagine you want to return a random element from the input list? You're just choosing a random integer based on the length of the input array. Try using another method from the random module
Ryan Felton
1,471 PointsRyan Felton
1,471 PointsI'm still not understanding