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 Letter Game Introduction

Abrar Fahim
Abrar Fahim
1,071 Points

syntax errors in letter game

I keep on getting a syntax error on line 34 even though I did everything he did

5 Answers

Hi there!

Could you paste in your code? If you've not done before, just put three back ticks before and after so it shows up formatted :)

Abrar Fahim
Abrar Fahim
1,071 Points
#Letter game

import random

#make a list of words
words = ["Batman",
         "Hulk",
         "Spider-man",
         "Flash",
         "Superman",
         "Wolverine"
    ]

while True:
    start = input("Press Enter to start game or press Q to quit: ")
    print("")
    if start == "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
#draw guessed letters and strikes
        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 guesses
#print out win/lose
        guess = input("Guess a letter")

        if len(guess) != 1:
              print("You can only guess once!!!")
              continue
        elif guess in bad_guess or guess in good_guess:
              print("You have already typed these in!!!")
              continue
        elif not guess.isalpha():
              print("You have to use 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))
        else:
              bad_guesses.append(guess)

    else:
           print("You couldnt guess the word! it is {}!!!".format(secret_word))

What's the error you get from python? I see that in this line:

elif guess in bad_guess or guess in good_guess:

you're checking bad_guess and good_guess, but earlier they're called:

bad_guesses = []
good_guesses = []

Although that should throw a NameError? Maybe I'm misreading the code sorry?

Abrar Fahim
Abrar Fahim
1,071 Points

I have fixed that but I still keep getting an error in line 34 about syntax error

cool, it's a bit hard to read, could you paste in lines around there? like 30 - 35? :)

Abrar Fahim
Abrar Fahim
1,071 Points
for letter in secret_word: #27
            if letter in good_guesses:
                print(letter, end ="")
            else:
                print("_", end = "")
        print("")
        print("Strikes {}/7".format(len(bad_guesses))
        print("") #34

Oh!

  print("Strikes {}/7".format(len(bad_guesses))

has three open brackets and two close brackets :)