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 trialtyler bucasas
2,453 Pointsrandom.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")
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
1 Answer
AJ Salmon
5,675 PointsHey 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
9
>>> example_function("Apple")
5
>>> example_function("Supercalifragilisticexpialidocious")
34
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 :)