HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Hangman in Python

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
pythonhangmanstackoverflow

Problem

I created this version of Hangman in Python 3. Does anyone have any tips for optimization or improvement?

import random
HANGMAN = (
'''
-------+''',
'''
       +
       |
       |
       |
       |
       |
-------+''',
'''
  -----+
       |
       |
       |
       |
       |
-------+''',
'''
  -----+
  |    |
       |
       |
       |
       |
-------+''',
'''
  -----+
  |    |
  O    |
       |
       |
       |
-------+''',
'''
  -----+
  |    |
  O    |
  |    |
       |
       |
-------+''',
'''
  -----+
  |    |
  O    |
 /|    |
       |
       |
-------+''',
'''
  -----+
  |    |
  O    |
 /|\   |
       |
       |
-------+''',
'''
  -----+
  |    |
  O    |
 /|\   |
 /     |
       |
-------+''',
'''
  -----+
  |    |
  O    |
 /|\   |
 / \   |
       |
-------+''')
MAX = len(HANGMAN) - 1
WORDS = ('jazz', 'buzz', 'hajj', 'fuzz', 'jinx', 'jazzy', 'fuzzy', 'faffs', 'fizzy', 'jiffs', 'jazzed', 'buzzed', 'jazzes', 'faffed', 'fizzed', 'jazzing', 'buzzing', 'jazzier', 'faffing', 'fuzzing')
sizeHangman = 0
word = random.choice(WORDS)
hiddenWord = list('-' * len(word))
lettersGuessed = []
print('\tHANGMAN GAME\n\t\tBy Lewis Cornwall\n')
while sizeHangman  to close.')
                    quit()
    else:
        print('Unfortunately, "' + guess + '" is not in my word.')
        sizeHangman += 1
    lettersGuessed.append(guess)
print('This is your hangman: ' + HANGMAN[sizeHangman] + 'You\'ve been hanged! My word was actually ' + word + '.')
input('Press  to close.')

Solution

You can avoid repeating guess = input('Guess a letter: ').lower() by changing the inner while loop to:

while True:
    guess = input('Guess a letter: ').lower()
    if guess in lettersGuessed:
        print('You\'ve already guessed that letter!')
    else:
        break


Instead of for i in range(len(word)) use:

for i, letter in enumerate(word):


You could also avoid indexing by i altogether by using a list comprehension. I'm not sure if this is as clear in this case, though.

hiddenWord = [guess if guess == letter else hidden for letter, hidden in zip(word, hiddenWord)]


Instead of hiddenWord.count('-') == 0 use:

'-' not in hiddenWord


The docs say quit() should not be used in programs. Your count('-') == 0 check is unnecessarily inside the for i loop. If you move it after the loop, you could use break to exit the main loop. You could add an else clause to the main with statement for dealing with the "You've been hanged" case. The main loop becomes:

while sizeHangman  to close.')

Code Snippets

while True:
    guess = input('Guess a letter: ').lower()
    if guess in lettersGuessed:
        print('You\'ve already guessed that letter!')
    else:
        break
for i, letter in enumerate(word):
hiddenWord = [guess if guess == letter else hidden for letter, hidden in zip(word, hiddenWord)]
'-' not in hiddenWord
while sizeHangman < MAX:
    print('This is your hangman:' + HANGMAN[sizeHangman] + '\nThis is the word:\n' + ''.join(hiddenWord) + '\nThese are the letters you\'ve already guessed:\n' + str(lettersGuessed))
    while True:
        guess = input('Guess a letter: ').lower()
        if guess in lettersGuessed:
            print('You\'ve already guessed that letter!')
        else:
            break
    if guess in word:
        print('Well done, "' + guess + '" is in my word!')
        for i, letter in enumerate(word):
            if guess == letter:
                hiddenWord[i] = guess
        if '-' not in hiddenWord:
            print('Congratulations! My word was ' + word + '!')
            break
    else:
        print('Unfortunately, "' + guess + '" is not in my word.')
        sizeHangman += 1
    lettersGuessed.append(guess)
else:
    print('This is your hangman: ' + HANGMAN[sizeHangman] + 'You\'ve been hanged! My word was actually ' + word + '.')
input('Press <enter> to close.')

Context

StackExchange Code Review Q#23678, answer score: 6

Revisions (0)

No revisions yet.