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 trialogechi1
14,455 Pointswhat's wrong with my random_member function here?
def random_member(the_list):
x = len(the_list) - 1
pick_a_number = random.randint(0, x)
return the_list[pick_a_number]
the above doesn't pass
this is the example a mod gave:
def random_member(some_list):
# x is the upper bound of the random function. it needs to equal the length of the list - 1 so we dont go out of index in the list
x = len(some_list) - 1
#num is the random number generated by the random function. it will be between 0 and the length of the list argument passed in - 1
num = random.randint(0,x)
# returns the list item at the random index
return some_list[num]
import random
def random_member(the_list):
return len(the_list)
def random_member(the_list):
x = len(the_list) - 1
pick_a_number = random.randint(0, x)
return the_list[pick_a_number]
1 Answer
Martin Cornejo Saavedra
18,132 PointsI copypasted your example and passed. But you have to remove the short function, it has no purpouse.
import random
#delete or comment this
#def random_member(the_list):
# return len(the_list)
def random_member(the_list):
x = len(the_list) - 1
pick_a_number = random.randint(0, x)
return the_list[pick_a_number]
ogechi1
14,455 Pointsogechi1
14,455 Pointsyup, the error i kept getting was that task 2 is no longer passing, so i pasted it in so i wouldn't have to keep re-entering it it passed in my console as well. thank you for the feedback martin!