Skip to content

Instantly share code, notes, and snippets.

@rukshn
Last active February 23, 2022 10:14
Show Gist options
  • Save rukshn/13ebed4319fa336153f4bb34e1c716a3 to your computer and use it in GitHub Desktop.
Save rukshn/13ebed4319fa336153f4bb34e1c716a3 to your computer and use it in GitHub Desktop.
A simple hangman game written in Python
import random
words = ['apple', 'crane', 'kane', 'lime', 'kite']
word = random.choice(words)
game = ['_']*len(word)
wrog_letters = []
lives = 6
print (game)
while lives > 0:
val = input("Guess the letter ? ")
if (val in word):
print ('Correct')
occs = [n for (n, e) in enumerate(word) if e == val]
for n in occs:
game[n] = val
if ('_' in game) == False:
print ('You Won')
break
else:
print ('Incorrect guess')
wrog_letters.append(val)
lives -= 1
print ("Wrong letters ")
print (wrog_letters)
print ("Game state ")
print (game)
print ("Lives ")
print( lives)
else:
print ('Game over, you are dead')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment