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 trialKenneth Phillips
Courses Plus Student 10,188 Pointsrandom_item index
Ok so I asked about this earlier and no one could help me. So I'm starting another discussion on the question. I don't understand why it won't work
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
def random_item(iterable):
index = random.randint(0, len(iterable) - 1)
return iterable[index]
1 Answer
Chris Howell
Python Web Development Techdegree Graduate 49,702 PointsYou mention it does not work but did not post an "error" message with it. If you attempted to run that that code AS IT IS in that snippet it is doing exactly what is has been told to do so far. You wrote valid code, defined a proper function but you never called that function so that function never executes any code.
But without running it, I can tell you immediately you will most likely get an error because you are missing the import for random.
This error isnt currently occurring because you defined the function
def random_item(iterable): # Defined a function call random_item that takes 1 parameter
# some logic here
But you never called that function and passed it that 1 parameter. Which could look something like this.
random_item('Treehouse') # This is calling the function. 'Treehouse' is the string, which can be iterated over.
Kenneth Phillips
Courses Plus Student 10,188 PointsKenneth Phillips
Courses Plus Student 10,188 PointsOk thanks. Yeah you're right I forgot to add import random but you were wrong about calling the function since the teachers usually don't make you do that. But yeah thank you.
Chris Howell
Python Web Development Techdegree Graduate 49,702 PointsChris Howell
Python Web Development Techdegree Graduate 49,702 PointsYou may not need to call the function for now to pass a challenge or a quiz section. Especially if you are learning how to write functions.
But functions only exist to be called upon to perform some important task. So in the very near future you will be calling functions ALL the time. A well defined function can be a very powerful thing and will become your best friend in programming. :-)
Kenneth Phillips
Courses Plus Student 10,188 PointsKenneth Phillips
Courses Plus Student 10,188 PointsNo yeah I agree with you. I will need to start calling the function in the future. I had already thought of that too. I'm just saying the teacher usually doesn't REQUIRE it for the purpose of getting the quiz done. But thanks for helping me out I really appreciate it.
Chris Howell
Python Web Development Techdegree Graduate 49,702 PointsChris Howell
Python Web Development Techdegree Graduate 49,702 PointsNo problem! Glad it helped!