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 trialRobert Peters
3,728 PointsHi, sorry i don't get this bit at all, could anyone explain?
not even sure what's really being asked!
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
import random
def random_item(iter(arg1)):
ran_num = random.randint(0, len(arg1) - 1)
return arg1[ran_num]
question is this btw: First, import the random library. Then create a function named random_item that takes a single argument, an iterable. Then use random.randint() to get a random number between 0 and the length of the iterable, minus one. Return the iterable member that's at your random number's index.
1 Answer
Chris Freeman
Treehouse Moderator 68,457 PointsYou are VERY close. Remove the iter()
wrapper from the parameter arg1
.
import random
def random_item(arg1): # <-- removed 'iter()' wrapper
ran_num = random.randint(0, len(arg1) - 1)
return arg1[ran_num]
Robert Peters
3,728 PointsRobert Peters
3,728 Pointscheers man, not a clue what the iter() wrapper actually is, just thought from the questions wording that it had to be specifically formatted as an iterable. ...Though i guess a string is an iterable as long as it's 2 or more characters long, right?
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsIn truth, even a one character string is iterable. A zero-length string is "False-y" so no iteration occurs.