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 trial

Python Python Basics (Retired) Pick a Number! Any Number! Imports

what'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]
imports.py
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
Martin Cornejo Saavedra
18,132 Points

I 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]

yup, 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!