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

Hannah Mackaness
Hannah Mackaness
4,437 Points

I don't know why I got this python function challenge right

there seemed to be a lot of complex instructions about printing number one less than length..I wasn't sure so I just put this and it passed..what is going on?

imports.py
import random
def random_member(iterate):
  return len(iterate)
li = [1,2,3,4,5]
print(random.choice(li))

[MOD: cleaned up auto-formatting -cf]

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,457 Points

Sometimes the challenges aren't too rigorous about the test conditions for passing. It turns out that the length of the input in Task 2 also happens to be an accepted value for a random number between 0 and the length of the test input in Task 3.

As a factious example, this code also passes the challange:

import random

def random_member(arg):
    return 3

As well as the len(arg) example, also passes:

import random

def random_member(arg):
    return len(arg)

Since the len(arg) is passing, maybe this challenge content needs to be revisited. Tagging Kenneth Love

You can actually pass this challenge with just this:

import random
def random_member(iterate):
  return len(iterate)

I would guess that the list they are using includes the same number as len(iterate). So if the length of the list is 10 and the number 10 is somewhere in the list then it will work. As long as your code returns an item from the list it will pass.

[MOD: added blank line before code block, added missing python for formatting -cf]

Martin Cornejo Saavedra
Martin Cornejo Saavedra
18,132 Points

This is a correct answer:

import random

def random_member(list):

  ran_num = random.randint(0, len(list)-1)
  return list[ran_num]

Yours wasn't correct but I don't know how it passed, maybe Abrahamsen is right.

[MOD: added blank line before and after code block for formatting -cf]