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

Help with this: Now, make random_member pick a random number between 0 and the length of the list... Imports Challenge

I dont understand why I can't pass this challenge, please help.

imports.py
import random 

def random_member(membersList):
  listLength = len(membersList)
  randomNum = random.randint(0, listLength)-1
  for i in membersList:
    if membersList[i] == randomNum:
      return i

1 Answer

Srikanth Srinivas
Srikanth Srinivas
1,465 Points
import random

def random_member(anylist):

  randnum = random.randint(0,(len(anylist)-1))
  return anylist[randnum]

This is the code you are expected to write. Firstly, in the randomNum line of your code, the "1" is outside the bracket. Next, why are you writing a for loop for the items in membersList? You can just return the item in the list if you know the index by writing anylist[index], where anylist is the name of your list, and the index is the index which the element in the list belongs to.

Hope this helps!

Please choose as best answer if this helped you. Good luck.