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 trialtest account
565 Pointsrandom.randint
Stuck on this one. I have tried 3 different (VERY SIMILAR) variations of the attached code/screenshot and am not getting it solved. I am thinking the problem is that the 'return'?
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
import random
def random_item(num):
random.randint(0, len(num - 1))
return(random.randint)
1 Answer
Steven Parker
231,271 PointsYes, partly. You want to return an element from the iterable. But to do that, when you call randint (the first time) you will need to save the value it returns in a variable. Then you can use that variable as an index to select an item from the iterable.
test account
565 Pointstest account
565 PointsI'm lost. don't know what you just said, but I tried this and it did not work.
import random def random_item(num): iterable = random.randint(0, len(num - 1)) random.randint(0, len(num - 1)) return(iterable)
Steven Parker
231,271 PointsSteven Parker
231,271 PointsIt's hard to understand that unformatted, but it looks like you're calling random.randint twice instead of just once. But it does look like you're saving the first call in a variable named "iterable". Instead of returning that directly, you should use it as an index value for the argument ("
num[iterable]
").And when posting code, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. Or watch this video on code formatting.