Python Python Basics (2015) Letter Game App Random Item

tyler bucasas
tyler bucasas
2,453 Points

random.randint() code challenge

I've been working on this challenge for awhile now and I dont fully understand what it is asking, someone pls help or at least let me know if im on the right track thank you :)
import random

def random_item("treehouse"):
    number = random.randint(0, len("treehouse")-1)
    return number("treehouse")

# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"

1 Answer

AJ Salmon
AJ Salmon
5,674 Points

Hey Tyler! You're using random.randint() correctly, but there are some problems with the way your function is structured. When you declare a function, or write it for the first time, the argument you give to it is just a placeholder. This is called setting the parameters; when the function is called, later on, it needs to be able to work with many different arguments, not just "treehouse". The one you give it when first defining it simply represents the actual argument. Here's an example:

>>> def example_function(placeholder):     # <- This is declaring the function
...     return len(placeholder)
>>> example_function("Treehouse")           # <- The following lines are calling, or using, the function
>>> example_function("Apple")
>>> example_function("Supercalifragilisticexpialidocious")

You can name the placeholder whatever you want; I called it 'placeholder' for illustrative purposes. If your function needed to take two arguments, then you'd give it two placeholders when setting the parameter. So, in your code, instead of using the actual string "treehouse", give random_item a placeholder argument, named whatever you want. Then use that placeholder exactly like you used "treehouse"! The challenge will call your function for you when you hit Check Work. Hopefully this clears it up!

Last thing- You just need to return number, not what you have written. number will be just that, a number, and that's what the challenge asks to return :)