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

César Jardim
César Jardim
4,847 Points

I keep getting the error "Task 2 is no longer passing". What am I doing wrong here?

I keep getting this error and found in the forums someone who passed with a similar solution. Can't tell whether this is a bug or I'm doing something wrong.

imports.py
import random
def random_member(lista):
  x = len(lista) - 1
  index = random.randint(0, x)
  return lista[index]

2 Answers

Gunhoo Yoon
Gunhoo Yoon
5,027 Points

I think it's bug which grader makes. Here's my reason.

This is passing code with same logic as yours except I didn't use extra variable.

import random

def random_member(li):
  i = random.randint(0, len(li) - 1)
  return li[i]

This is not passing code but copy of above code except I used random variable called bla which is never used.

import random

def random_member(li):
  bla = 'sdfsdfsd'
  i = random.randint(0, len(li) - 1)
  return li[i]

So I'm just guessing that grader gets sensitive when it sees extra variable like that.

I think its just a bug.

because this worked for me.

#step 1

import random

# step 2  

def random(my_list):
    return len(my_list)

# step 3

def random_member(my_list):
  endrange=len(my_list) - 1
  myindex = random.randint(0,endrange)
  return my_list[myindex]