Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

Guilherme Oliveira
PLUS
Guilherme Oliveira
Courses Plus Student 2,985 Points

Python Basics - Stage 6 (Challenge Task 2 of 2)

Having a hard time trying to figure it out the right way to do this. I don't really know where the error is:

import random

my_list = [1, 2, 3, 4, 5]

def random_member(my_list):

random_num = randint(0, (len (my_list) - 1))

my_list[random_num]

6 Answers

Peter Szerzo
Peter Szerzo
22,661 Points

Guilherme,

These should do the trick:

  • indent all lines after the def random_member(my_list) one. You need to do this to tell python that those two lines are part of the function definition.

  • instead of randint, type random.randint so it references the module you are importing in the first line (otherwise Python will not find it).

  • change the last line to 'return my_list[random_num]'. Otherwise, Python will find the random array member, but will never tell the rest of the code about it. Essentially, it takes the secret to the grave.

Let me know if this works. Happy learning!

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

You don't need to provide the my_list variable. That'll get passed in by the test runner (it's OK to provide it, though, in your own scripts.

Your example above isn't indented at all. Is your submission to the CC indented correctly for creating a function or is it just like what you have above?

Also, what error(s) are you getting, if any?

Martin Luckett
Martin Luckett
32,591 Points

Try using random.randint not just randint

Mart

Just to add to Kenneth Love 's answer, even when I completely doubted my code, most of my issues in the Python challenges were just down to the indenting. As an aside I quite like how it's beating some of my bad coding layout habits out of me.

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

Just wait'll we cover PEP 0008 in a later course :D

Guilherme Oliveira
PLUS
Guilherme Oliveira
Courses Plus Student 2,985 Points

Got it! Thank you Kenneth, Peter, Martin and Andrew for your help. It was the indentation. Apparently the biggest mistakes in coding for beginners are in the fundamentals. We get caught up so much with the challenge itself sometimes that we don't see the obvious.

U am badly struggling do not get this at all

I do not understand what it means by int arguement

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

It means the function will get an argument that is an int.

Would I write the arguement as a = 4

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

No, you don't write the arguments, you just say how many come in.

For example:

def takes_no_args():
    pass


def takes_one_arg(something):
    pass


def takes_two_args(something, something_else):
    pass

takes_one_arg() takes only one argument. takes_two_args() requires two arguments. And takes_no_args() doesn't accept any arguments.

Ah ha I understand thank you so much

import random

def random_did(a): a = 4 return random.radiant (1, a)

I did this but it still did not work I indented

import random

def random_did(a): a = 4 return random.radiant (1, a)

I did this but it still did not work I indented

Kenneth Love
Kenneth Love
Treehouse Guest Teacher

That won't pass the challenge because that's not what the challenge is asking for.