Skip to content

Instantly share code, notes, and snippets.

@moretti
Created April 29, 2013 13:11
Show Gist options
  • Save moretti/5481496 to your computer and use it in GitHub Desktop.
Save moretti/5481496 to your computer and use it in GitHub Desktop.
import random
HANGMANPICS = ["""
+---+
| |
|
|
|
|
=========""", """
+---+
| |
O |
|
|
|
=========""", """
+---+
| |
O |
| |
|
|
=========""", """
+---+
| |
O |
/| |
|
|
=========""", """
+---+
| |
O |
/|\ |
|
|
=========""", """
+---+
| |
O |
/|\ |
/ |
|
=========""", """
+---+
| |
O |
/|\ |
/ \ |
|
========="""]
words = 'spam ham eggs python zen beautiful explicit simple hack hacking divide conquer programming random method arithmetic addition subtraction multiplication'.split()
secret_word = words[random.randint(0, len(words) - 1)]
hidden_word = '-' * len(secret_word)
letters_tried = []
guess_count = 0
max_guesses = len(HANGMANPICS) - 1
print("Hangman")
while guess_count < max_guesses:
print(HANGMANPICS[guess_count])
print(hidden_word)
while True:
print('Your guess:')
guess = input().lower()
if len(guess) != 1 or guess not in 'abcdefghijklmnopqrstuvwxyz':
print("You can only guess a single LETTER at a time. Please choose again.")
elif guess in letters_tried:
print("You already picked that letter. Please choose again.")
else:
break
letters_tried.append(guess)
if guess in secret_word:
tmp = list(hidden_word)
for i in range(len(secret_word)):
if secret_word[i] == guess:
tmp[i] = guess
hidden_word = ''.join(tmp)
if hidden_word == secret_word:
print("You guessed the word: " + secret_word);
print("You win!");
break
else:
print("There are no " + guess + "'s in the word");
guess_count += 1
if guess_count == max_guesses:
print(HANGMANPICS[guess_count])
print("You have run out of guesses!")
print("The word was: " + secret_word)
print("You lose.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment