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

Tariq Zidan
Tariq Zidan
14,029 Points

Python Hangman Letter Guessing Game... I'm all out of guesses.

I'm trying to run the hangman script. I get a syntax error at the break at line 56. When I remove the break I get a syntax error at the else statement on line 57. Of course I'm biased, but I can't find what needs to be fixed.

import random

make a list of words\

words = [ 'apple' 'banana' 'orange' 'coconut' 'strawberry' 'lime' 'grapefruit' 'lemon' 'kumquat' 'blueberry' 'melon' ]

while True: start = input("Press enter to start or Q to quit.") if start.lower() == "q": break

# pick a random word
secret_word = random.choice(words)
good_guesses = []
bad_guesses = []

while len(bad_guesses) < 7 and good_guesses != len(list(secret_word)):

    # draw spaces
    # print good guesses and strikes
    for letter in secret_word:
        if letter in good_guesses:
            print(letter, end=' ')
        else:
            print('-', end=' ')
    print('')
    print('Strikes: {}/'.format(len(bad_guesses)))
# 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 guessed that.")
        continue
    elif not guess.isalpha():
        print("You can only guess letters.")
        continue

        # print out win/lose
    if guess in secret_word:
        good_guesses.append(guess)
        if len(good_guesses) == len(list(secret_word)):
            print("You win. The secret word is {}".format(list(secret_word))
            break               
    else:
        bad_guesses.append(guess)
else:
    print("You ran out of guesses. the secret word is {}.".format(secret_word))
Steven Parker
Steven Parker
231,236 Points

It's hard to read incompletely formatted code, particularly Python. For future questions, use the snapshot function in the workspace (camera icon) and share the link it creates.

2 Answers

andren
andren
28,558 Points

Taking a quick glace at you code I'm going to guess the issue lies in this line:

print("You win. The secret word is {}".format(list(secret_word))

You call three functions but only close two of them, meaning you are lacking a closing parenthesis at the end of the line. If you add that like this:

print("You win. The secret word is {}".format(list(secret_word)))

Then I'm guessing that error won't occur. I would normally test my solutions before posting them but as Steven has noted the formatting on your code is somewhat messed up in your post so I can't really test it that easily.

Tariq Zidan
Tariq Zidan
14,029 Points

Thanks it was the missing parentheses.