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!
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
Zachary Carlos
912 PointsWhy is my letter game not running properly?
my function doesn't properly append bad guesses into the function.
import random
fruits = [ 'strawberry', 'melon', 'apple', 'blueberry', 'grape', 'lemon', 'starfruit', 'banana', 'orange', 'lime', ]
while True: start = input("Press enter/return to start, or hit Q to quit the game.") if start.lower() == 'q': break
secret_word = random.choice(fruits)
bad_guesses = []
good_guesses = []
while len(bad_guesses) < 7 and len(good_guesses) != len(list(secret_word)):
for letter in secret_word:
if letter in good_guesses:
print(letter, end='')
else:
print('_', end='')
print('')
print("Strike {}/7".format(len(bad_guesses)))
print('')
guess = input("Guess a letter: ").lower()
if len(guess) != 1:
print("You can only guess one letter at a time!")
continue
elif guess in good_guesses or bad_guesses:
print("You've already used that letter!")
continue
elif not guess.isalpha():
print("You can only guess letters!")
continue
elif guess in secret_word:
good_guesses.append(guess)
if len(good_guesses) == len(list(secret_word)):
print("You win! The secret word was {}!".format(secret_word))
break
else:
bad_guesses.append(guess)
else:
print("You didnt get it! My secret word was {}.".format(secret_word))
1 Answer

Aaron Loften
8,303 PointsFirst off, pretty cool program! I think it needs some work, like....the first message says to hit enter or return but only "q" starts the game.
Second...Im not sure why it broke originally, but I fixed it. I did this by repairing your if statement. Your second if statement when checking their guess checks to see if the letter exists in good_guesses or if bad_guesses is true(assuming it works like javascript as I havent gotten to truthy stuff yet or if statements in python). I think you meant to say psuedo code[if letter is in good guesses or if letter is in bad guesses].
Basically... when searching if something is true, you have to check two independent conditions. We cant check to see if "x is equal 5 or 4" we have to check to see if "x is equal to 5 or x is equal to 4." Does that make sense? Its a weird concept. :p
Ive rewritten the portion of code that needed it and I think itll work for you now. :)
import random
fruits = [ 'strawberry', 'melon', 'apple', 'blueberry', 'grape', 'lemon', 'starfruit', 'banana', 'orange', 'lime', ]
while True:
start = input("Press enter/return to start, or hit Q to quit the game.")
if start.lower() == 'q':
break
secret_word = random.choice(fruits)
bad_guesses = []
good_guesses = []
while len(bad_guesses) < 7 and len(good_guesses) != len(list(secret_word)):
for letter in secret_word:
if letter in good_guesses:
print(letter, end='')
else:
print('_', end='')
print('')
print("Strike {}/7".format(len(bad_guesses)))
print('')
guess = input("Guess a letter: ").lower()
if len(guess) != 1:
print("You can only guess one letter at a time!")
continue
elif guess in good_guesses or guess in bad_guesses:
print("You've already used that letter!")
continue
elif not guess.isalpha():
print("You can only guess letters!")
continue
elif guess in secret_word:
good_guesses.append(guess)
if len(good_guesses) == len(list(secret_word)):
print("You win! The secret word was {}!".format(secret_word))
break
else:
bad_guesses.append(guess)
else:
print("You didnt get it! My secret word was {}.".format(secret_word))

Aaron Loften
8,303 PointsAs another example... If your code said "if door was unlocked or open: walk through the door" the program would not know what to do with open. Is it saying if the door is open? If open is a word? If the window is open? who knows!
Zachary Carlos
912 PointsZachary Carlos
912 Pointsfor some reason part of my code isnt appearing in the picture, some of it is on top of the picture.