Skip to content

Instantly share code, notes, and snippets.

@devpruthvi
Last active August 29, 2015 14:07
Show Gist options
  • Save devpruthvi/35c90b981e507d3c54f1 to your computer and use it in GitHub Desktop.
Save devpruthvi/35c90b981e507d3c54f1 to your computer and use it in GitHub Desktop.
import random
import string
availablewords = ['anaconda','all of me','rude','chandelier','guts over fear','maps']
punc = set(string.punctuation)
hangman = "HANGMAN"
tried = set()
def hangmangame():
again = True
while again:
alreadySaid = set(['a','e','i','o','u','A','E','I','O','U'])
guessWord = random.choice(availablewords)
displayword = ""
for each in guessWord:
if each in alreadySaid or each in punc:
displayword += each
elif each == ' ':
displayword += '/'
else:
displayword += "_"
matchingguessword = ''
for each in guessWord:
if each == ' ':
matchingguessword += '/'
else:
matchingguessword += each
print(" ".join(displayword))
chances = 7
guessed = False
while not guessed and chances > 0:
whatplayersaid = input("Guess a letter: ")
displayword = ''
if whatplayersaid in guessWord:
alreadySaid.add(whatplayersaid)
for char in guessWord:
if char in alreadySaid or char in punc:
displayword += char
elif char == ' ':
displayword += '/'
else:
displayword += '_'
tried.add(whatplayersaid)
if displayword == matchingguessword:
guessed = True
else:
if whatplayersaid not in tried:
chances -= 1
tried.add(whatplayersaid)
print("Nope.", chances, "chances left.")
print(hangman[0:(7-chances)])
print(" ".join(displayword))
if chances == 0:
print("You LOST, The word is: " + guessWord + " --> " + matchingguessword)
again = (input("Again [y/n]: ").lower() == 'y')
hangmangame()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment