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 trialSam Royal
564 PointsNot sure what to retrun, if the rest of the code's even correct.
It says to return the iterable member that's at your random number's index.
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
import random
def random_item("Hello")
random.randint(0, (len(Hello) - 1))
return
Will Roberts
Courses Plus Student 1,498 Pointsran_number = random.randint(0, (len(Hello) - 1)) return Hello[ran_number]
2 Answers
Jennifer Nordell
Treehouse TeacherHi there, Sam! Will Roberts is correct. Your function definition is missing a semicolon which will produce a syntax error. But above and beyond that, your code is has a string literal in the place where it should have a parameter. The parameter should be a valid Python variable name. Any information sent into the function will now have that value assigned to that parameter name. Also, because that's a string literal, your attempt to get the length of the variable Hello
will fail, as that variable has not been declared. This happens when you're producing the random number. The random number should produce a valid index in the range given. What will be returned will be the whatever is in the index spot of the iterable that was sent in. But we have no idea what is being sent. Just that some iterable is being sent.
Here's how I did it:
import random
def random_item(word): #define the function that takes some iterable
index = random.randint(0, (len(word) - 1)) #get a random number within the appropriate range
return word[index] #return the value that resides at that index of whatever iterable was sent
Hope this helps!
Will Roberts
Courses Plus Student 1,498 PointsYour missing your colon when creating your function also random.randint(0, (len(Hello) - 1)) is not assigned to a variable then you need to return Hello[<your_variable>]
Jennifer Nordell
Treehouse TeacherHi there! I changed your comment to an answer as it will mark the question answered in the forums and allow for voting.
Sam Royal
564 PointsSam Royal
564 PointsReturn*