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 trialMax Phelan
1,398 PointsCant figure out were my mistake is. Can anyone help spot it.
import random
# make a list of words
words = [
'apple'
'bananas'
'orange'
'coconut'
'strawberry'
'lime'
'grapefruit'
'lemon'
'kumquat'
'blueberry'
'melon'
]
while True:
start = input("Press enter/return to start, or enter Q to quit")
if start.lower() == 'q':
break
# pick a random word
secret_word = random.choice(words)
bad_guesses = []
good_guesses = []
while len(bad_guesses) < 7 and len(good_guesses) != len(list(secret_word)):
# draw spaces
for letter in secret_word:
if letter in good_guesses:
print(letter, end='')
else:
print('_', end='')
print('')
print('Strikes: {}/7'.format(len(bad_guesses)))
print('')
# take guess
guess = input("Guess a letter: ").lower()
if len(guess) != 1:
print("You can only guess a single letter:")
continue
elif guess in bad_guesses or guess in good_guesses:
print("You've already guess that letter:")
continue
elif not guess.isalpha():
print("You can only guess letters!")
continue
if guess in secret_word:
good_guesses.append(guess)
if len(good_guesses) == len(list(secret_word)):
print("You win! The word was {}".format(secret_word))
break
else:
bad_guesses.append(guess)
else:
print("You didn't guess it! My secret word was {}".format(secret_word))
1 Answer
Steven Parker
231,269 PointsYou might need to describe the issue that led you to conclude there was a "mistake".
But (and perhaps this is it), I did notice that you compare the number of good guesses with the length of the word. This will only work as a strategy to detect a win if the word has only unique letters. For a word like "apple", you'll need a different strategy since you can win with 4 guesses but it has 5 letters.
Max Phelan
1,398 PointsMax Phelan
1,398 PointsThanks so much Steven. Sorry, yes i should of included the issue. Essentially when i run the program in the console a large number of underscores '_' get's printed, a much greater amount then any number of letters in any of the words. When letters are guessed the underscores get replaced, but its like the whole underscore line is just the list of words joined together.
Example of start of program:
Max Phelan
1,398 PointsMax Phelan
1,398 PointsFound the mistake. Forgot to add a comma between each word in the list. Cant believe i missed that! Thanks for your help
Steven Parker
231,269 PointsSteven Parker
231,269 PointsGood deal. I hope my other suggestion will be helpful.
Happy coding!