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

Python Python Basics (2015) Letter Game App Random Item

Brandon Harrison
Brandon Harrison
1,283 Points

how 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.

item.py
# 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
Wairton Rebouças
8,225 Points

Hi 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]
Brandon Harrison
Brandon Harrison
1,283 Points

thank you. I did not realize i could throw the len(iterable) inside of the randint so that helps a lot.

It 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
Brandon Harrison
1,283 Points

yeah you made a point lol. the words are definitely not enough but it was still explained very nicely so thank you.