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

what am i doing wrong here?

maybe im still a little confused from previous video what this is saying?? len(good_guesses) != len(list(secret_word) I think the first part says the amount of good guesses is not equal to the second part, but this is where i get confused. Plus the random challenge. Please help.

item.py
# EXAMPLE
# random_item("Treehouse")
# The randomly selected number is 4.
# The return value would be "h"
import random

def random_item('item'):
    number = random.randint(0, len(item) - 1)
    return number
Eric Hodgins
Eric Hodgins
29,207 Points

In the challenge you have to return a random letter from a string that's passed into the function. Right now you're passing in the hard coded string 'item'. So just change that so you can handle any string that's passed in. You're also returning the random number. You want a random letter from the string passed in using the random number.

import random

def random_item(item): # now you can pass in any string you want. Whether it be 'item' or 'items' or 'pineapple'
    number = random.randint(0, len(item) - 1)
    return item[number] # returns the random letter NOT the random number.

Does that make more sense?

Yeah that makes a little bit more sense but what about len(good_guesses) != len(list(secret_word) I think the first part says the amount of good guesses is not equal to the second part, but what does the second part say??

Eric Hodgins
Eric Hodgins
29,207 Points

I haven't watched the video. But the second part with this:

len(good_guesses) != len(list(secret_word) )

So list says basically make an array. For example: list("secret") becomes ['s', 'e', 'c', 'r', 'e', 't']. It's just a convenient way to make an array. Then you can get the length from that as well. You can play around with that in a python repl as well. Is that what you were wondering?

Hey Eric, I tried the code and it came back with an error, sorry to bother you with all this. just trying to understand it all before moving on if i can.

Eric Hodgins
Eric Hodgins
29,207 Points

No worries!, Sorry I forgot the 0 in the randint function!

I dont think so Eric, i guess i didnt give you enough info. words = ['apple', 'banana', 'etc']
secret_word = random.choice(words)

no problems here eric, I appreciate all the help i can get. But what do you think about the len(list(secret_word)) secret_word is a variable that stores a randomly chosen word by the computer?? I been trying to figure out what this statement len(list(secret_word)) says.

Eric Hodgins
Eric Hodgins
29,207 Points

From what I gather you'd be correct. secret_word is randomly chosen from the words array. Then turned into a list and then the length is found.

1 Answer

Cindy Lea
PLUS
Cindy Lea
Courses Plus Student 6,497 Points

You need to simpplify some of the staements & formats:

''' import random

def random_item(iterable): return iterable[random.randint(0, len(iterable)-1)] '''

Thanks Cindy, what about len(good_guesses) != len(list(secret_word) I think the first part says the amount of good guesses is not equal to the second part, but what does the second part say??