Python Python Basics (2015) Letter Game App Random Item

Alexandra Ciorra
Alexandra Ciorra
Python Web Development Techdegree Student 796 Points

Still confused.

What am I missing?

item.py
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
Steven Parker
170,448 Points

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

Alexandra Ciorra
Alexandra Ciorra
Python Web Development Techdegree Student 796 Points

Thanks 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
Steven Parker
170,448 Points

It 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
Alexandra Ciorra
Python Web Development Techdegree Student 796 Points

Thank you Steven! Your explanation was very helpful and I was able to figure it out properly and understand it.

Ben Hedgepeth
Ben Hedgepeth
Python Web Development Techdegree Student 6,148 Points

For 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?