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 trial![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Brandon Harrison](https://secure.gravatar.com/avatar/6a1d76856e938b17b9ad24eafc0a731b?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Brandon Harrison
1,283 Pointshow do I code out random.choice without using it?
I don't feel like I learned how to do this during the course and I cannot figure out what to do. I have tried many things and things that do not even make sense. Was I just not paying attention or what? Without giving me the answer, can someone explain this to me differently? my last bit of code there is me giving up by the way.
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
import random
def random_item("Treehouse")
random.randint(0,8)
return random_item(len())
2 Answers
![Wairton Rebouças](https://secure.gravatar.com/avatar/d24082caee914effe3d127c1a2177bcb?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Wairton Rebouças
8,225 PointsHi Brandon,
(I'm from Brazil, I hope you can understand my english :P) Let's examine your code: def random_item("Treehouse"), ok you've used def keyword correctly and gave the right name to your function. But you forgot ':' at the end of the statement and you defined a value as argument, which is syntatically wrong. The correct form is something like this: def random_item(iterable): I recommend you to watch again the video about function definition.
The instruction says: "Then use random.randint() to get a random number between 0 and the length of the iterable, minus one" so, instead of "random.randint(0,8)" you should have done something like "index = random.randint(0, len(iterable) - 1)" this will give you a index inside of the iterable (a list is an example of iterable) and the task asks you to return the item at the index generated by the previous call, so it should be something like this "return random_item(index)"
I'll post a solution here just to help you to see the differences (but try to understand no just copy and paste):
import random
def random_item(iterable):
index = random.randint(0, len(iterable) - 1)
return iterable[index]
![crosscheckking](https://uploads.teamtreehouse.com/production/profile-photos/5944162/micro_IceKing.webp)
crosscheckking
20,110 PointsIt would be easier to give you the answer and explain it, but here's my attempt at not giving everything away. For the parameter, you will need to use basically a variable. This is so that whatever is passed to your function will be contained within the variable. It will pass it's own iterable to your function when checking it. This means we do not know the length of iterable so we cannot hardcode it like in you're a attempt. You will need to use the len() function passing the parameter as the argument and subtracting 1 from this. Lastly, it wants you to return the item at the index randomly selected for the parameter. This would be the parameter[index] that you would have to return. I could give you the full answer and explain it better that way so you could go through it step by step, but I tried not to give everything away so you could try it again.
![Brandon Harrison](https://secure.gravatar.com/avatar/6a1d76856e938b17b9ad24eafc0a731b?s=60&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Brandon Harrison
1,283 Pointsyeah you made a point lol. the words are definitely not enough but it was still explained very nicely so thank you.
Brandon Harrison
1,283 PointsBrandon Harrison
1,283 Pointsthank you. I did not realize i could throw the len(iterable) inside of the randint so that helps a lot.