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 trialOmari Wright
1,475 PointsCant understand why this code is not working?
EXAMPLE
random_item("Treehouse")
The randomly selected number is 4.
The return value would be "h"
import random def random_item("iterable") random.randint(0, (len(iterable)-1)) return index.randint()
# EXAMPLE
import random
def random_item("iterable"):
random.randint(0, len(iterable) - 1)
return index.randint()
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
1 Answer
Cameron Nilon
15,601 PointsHey Omari,
Let's break it down a bit. Remember to index an iterable you can use '[]' in other words, say you have "hello" and you want to get the letter 'e', you would get this by indexing with square brackets [1], remember it is zero indexing. Now to use randint() you basically have that correct, except iterable is not defined. randint() returns a number between your first arg and your second arg. Also, I wanted to point out that your parameter is syntactically incorrect, lose the quotations or assign "iterable" to word without quotations ie iterable = "iterable".
Allow me to give you an example on how you can do this challenge.
import random
def random_item(arg):
return arg[random.randint(0, len(arg)-1)]