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 trialSrikanth Srinivas
1,465 Pointshow do i get forward with this question?
so I'm supossed to get random_member pull out a number from 0 to the length of the items in my list - 1 as an index for searching my list?
import random
list = []
def random_member(list):
return len(list)
x = len(list)
return list[random.randint(0,(x-1))]
2 Answers
Kenneth Love
Treehouse Guest TeacherActually, you found a bug in the code challenge. I've fixed it and your code should work now.
Kenneth Love
Treehouse Guest TeacherAnytime you do return
in a function, the function stops and sends back whatever you told it to. So your random_member
function, right now, always sends back the length of the provided list.
Srikanth Srinivas
1,465 Pointsso if i delete the line with return len(list), and just assign the length to a variable x, it should work, correct?
Kenneth Love
Treehouse Guest TeacherNot necessarily. I don't want to give the whole answer away. Your code is 99% there, though. But, remember, we're not wanting a list out of this, we want an item from the list.
Srikanth Srinivas
1,465 Pointsimport random
list = []
def random_member(list):
index = random.randint(0,(len(list)-1))
return list[index]
print (random_member([1,2,5,4,6,7,8,9]))
wh en i try this in an online py compiler, it runs exactly like the prompt suggests it should :( I really don't get whats wrong with the code, sorry for my newbie-ness
Srikanth Srinivas
1,465 PointsSrikanth Srinivas
1,465 PointsIt does! Thanks so much! Thoroughly enjoying the python course by the way :)