Charles Monaghan741 Points
I was able to get this far, am I close?
Please don't give me the full answer, just tell me what I'm doing theoretically wrong :)
Any help is appreciated.
# EXAMPLE # random_item("Treehouse") # The randomly selected number is 4. # The return value would be "h" import random def random_item(iterable): return random.randint(0, len(iterable)) - 1
Chris FreemanTreehouse Moderator 56,387 Points
You are close. You've generated the required random number. Now use that number as the index to select an item from the iterable and return that item.
Also, you should probably put the "-1" inside the
randint argument with the
len(iterable) instead of subtracting 1 after generation the number. Otherwise, if the
randint was 0, you'd be left with a negative index (which would work but not for the reasons you want).
Post back if you need more help. Good luck!!
David Evans9,796 Points
So to not give the answer, I'll just draw your attention to the last line of the instructions.
Return the iterable member that's at your random number's index.
What you have done so far has gotten the index as random.randint(x,y) returns an integer. ( You will need to adjust your minus 1, its not quite in the correct place. )
You'll need to subtract one from the iterable, and not the random number you generate.
Once you fix the subtraction from the length of the iterable not the random number generated, keep in mind that you just generated an integer which will be your index and then you'll need to find the 'iterable member' or the single character that is associated with the number you generated.