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 trialAlexandra Ciorra
Python Web Development Techdegree Student 796 PointsStill confused.
What am I missing?
import random
def random_item('Treehouse'):
num = random.randint(0, len('Treehouse') -1)
return num.index('Treehouse')
# The randomly selected number is 4.
# The return value would be "h"
2 Answers
Steven Parker
231,236 PointsYou should not put quotes around variable names. Doing that creates a string literal that represents the name itself instead of the variable's value.
Also, you won't want to use the "index" method here, that's a string method that performs a search and returns an index. Since you have an index already in "num", you just need to apply it to the iterable using bracket notation to get a specific item.
I'll bet you can get it now without an explicit code spoiler.
Ben Hedgepeth
Python Web Development Techdegree Student 10,287 PointsFor the challenge you don't necessarily need to pass in an argument. What is required is writing a function definition that can take any applicable iterable and have the function apply some work to it in the function body and return a value.
For instance, I used word
as the parameter when I did the challenge. It's a semantic placeholder so if anyone reads your code they have an idea of what type of input is provided to the function.
Did you remove all quote marks for all parameters? Did you revise your code as Steven pointed out?
Alexandra Ciorra
Python Web Development Techdegree Student 796 PointsAlexandra Ciorra
Python Web Development Techdegree Student 796 PointsThanks for not showing a spoiler. I became confused because it said iterable I thought iterable was a list or a string. now its saying that random_item is not defined. I have taken away the quotes and have done more video reviews but can't seem to find what piece is missing. Can you possibly give me a clue so hopefully I can figure it out? Thanks!! :)
Steven Parker
231,236 PointsSteven Parker
231,236 PointsIt sounds like you may not have applied the 2nd hint yet, using "num" as an index instead of calling a method named "index" on it (number variables don't have such a method anyway).
If you still need help, I can be more specific if you show the code you have now.
Alexandra Ciorra
Python Web Development Techdegree Student 796 PointsAlexandra Ciorra
Python Web Development Techdegree Student 796 PointsThank you Steven! Your explanation was very helpful and I was able to figure it out properly and understand it.