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

Guilherme Oliveira
Courses Plus Student 2,985 PointsPython 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
22,661 PointsGuilherme,
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
Treehouse Guest TeacherYou 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
32,591 PointsTry using random.randint not just randint
Mart

Andrew Molloy
37,259 PointsJust 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
Treehouse Guest TeacherJust wait'll we cover PEP 0008 in a later course :D

Guilherme Oliveira
Courses Plus Student 2,985 PointsGot 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.

Piraveen Partheepan
Full Stack JavaScript Techdegree Student 1,130 PointsU am badly struggling do not get this at all

Kenneth Love
Treehouse Guest TeacherWhat problems are you having?

Piraveen Partheepan
Full Stack JavaScript Techdegree Student 1,130 PointsI do not understand what it means by int arguement

Kenneth Love
Treehouse Guest TeacherIt means the function will get an argument that is an int.

Piraveen Partheepan
Full Stack JavaScript Techdegree Student 1,130 PointsWould I write the arguement as a = 4

Kenneth Love
Treehouse Guest TeacherNo, 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.

Piraveen Partheepan
Full Stack JavaScript Techdegree Student 1,130 PointsAh ha I understand thank you so much

Piraveen Partheepan
Full Stack JavaScript Techdegree Student 1,130 Pointsimport random
def random_did(a): a = 4 return random.radiant (1, a)
I did this but it still did not work I indented

Piraveen Partheepan
Full Stack JavaScript Techdegree Student 1,130 Pointsimport 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
Treehouse Guest TeacherThat won't pass the challenge because that's not what the challenge is asking for.