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 trialAndrew Heggie
6,348 PointsPython Basics Letter Guess Game
Hi Treehouse community,
Would someone please help me out. I have been getting this error time and time again with my code and I don't understand why it comes up:
My error is:
File "Letter_game.py", line 47
def get_guess(bad_guesses, good guesses):
^
SyntaxError: invalid syntax
My code is: import os import sys import random
make a list of words
words = [ 'apple', 'banana', 'organe', 'coconut', 'strawberry', 'lime', 'grapefruit', 'lemon', 'kumquat', 'blueberry', 'melon' ]
def clear(): if os.name == 'nt': os.system('cls') else: os.system('clear')
def draw(bad_guesses, good_guesses, secert_word):
clear()
print('Strikes: {}/7'.format(len(bad_guesses)))
print('')
for letter in bad_guesses:
print(letter, end=' ')
print('\n\n')
# draw spaces, guessed letters and strikes
for letter in secert_word:
if letter in good_guesses:
print(letter, end='')
else:
print('_', end='')
print('')
def get_guess(bad_guesses, good guesses): # take guess while True: guess = input("Guess a letter: ").lower()
if len(guess) != 1:
print("You can only guess a single letter")
continue
# checks to see if they are in lists
elif guess in bad_guesses or guess in good_guesses:
print("You have already guessed that letter")
continue
# isalpha checks if characters are letters
elif not guess.isalpha():
print("You can only guess letters!")
continue
else:
return guess
def play(done): clear() secert_word = random.choic(words) bad_guesses = [] good_guesses = []
while True:
draw(bad_guesses, good_guesses, secert_word)
guess = get_guess(bad_guesses, good_guesses)
if guess in secert_word:
good_guesses.appened(guess)
found = True
for letter in secert_word:
if letter not in good_guesses:
found = False
if found:
print("You win!)
print("The secert word was {}".format(secert_word))
done = True
else:
bad_guesses.appened(guess)
if len(bad_guesses) == 7:
draw(bad_guesses, good_guesses, secert_word)
print('You lost!')
print('The secert word was {}'.format(secert_word)
done = True
if done:
play_again = input('Play again? Y/n ').lower()
if play_again != 'n'
return play(done=False)
else:
sys.exit()
print('Welcome to Letter Guess')
done = False
while True: clear() welcome() play(done)
1 Answer
Mike Wagner
23,559 PointsThe first major error is in your def get_guess(bad_guesses, good guesses):
it's missing an underscore in "good_guesses" which is making the function definition invalid syntax. If you fix that, you should be able to sort out the rest of the errors with minimal difficulty
Mike Wagner
23,559 PointsMike Wagner
23,559 PointsI'm gonna go out on a limb and say that it has something to do with the typos in your code. There are many places where you spell
append()
asappened
,secret
assecert
and so on. Sinceget_guesses()
relies on at least theappend()
method working properly, I would suggest you go back through your code and try to fix these errors. If it still does not work, perhaps you can try to reformat your question so that I can view the code properly.