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 trialchrishill4
1,567 Pointswhat 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.
# 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
chrishill4
1,567 PointsYeah 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
29,207 PointsI 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?
chrishill4
1,567 PointsHey 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
29,207 PointsNo worries!, Sorry I forgot the 0 in the randint function!
chrishill4
1,567 PointsI dont think so Eric, i guess i didnt give you enough info. words = ['apple', 'banana', 'etc']
secret_word = random.choice(words)
chrishill4
1,567 Pointsno 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
29,207 PointsFrom 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
Courses Plus Student 6,497 PointsYou need to simpplify some of the staements & formats:
''' import random
def random_item(iterable): return iterable[random.randint(0, len(iterable)-1)] '''
chrishill4
1,567 PointsThanks 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??
Eric Hodgins
29,207 PointsEric Hodgins
29,207 PointsIn 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.
Does that make more sense?