Skip to content

Instantly share code, notes, and snippets.

@ecounysis
Created November 17, 2016 22:01
Show Gist options
  • Save ecounysis/9a6840697c31e8620a4ec03980238ad4 to your computer and use it in GitHub Desktop.
Save ecounysis/9a6840697c31e8620a4ec03980238ad4 to your computer and use it in GitHub Desktop.
# I wrote this after I answered this question on SO
# http://stackoverflow.com/q/40663103/102022
def hangman(word):
word = word.lower()
right_letters = []
wrong_letters = []
def guess(letter):
letter = letter.lower()
if letter in right_letters or letter in wrong_letters:
print "You've already guessed this letter"
elif letter in word:
print("You guessed a letter correctly.")
right_letters.append(letter)
else:
print("You guessed incorrectly.")
wrong_letters.append(letter)
def display(letter):
if letter in right_letters:
return letter
else:
return "_"
while len([contained for contained in [x in right_letters for x in list(word)] if contained]) < len(word):
print("Word: " + " ".join([display(letter) for letter in word]))
print("Make a guess")
guess(raw_input())
print("\n\n\n\n\n")
print("Wrong letters: " + " ".join(wrong_letters))
print("Word: " + " ".join([display(letter) for letter in word]))
print("You won!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment