Last active
February 23, 2022 10:14
A simple hangman game written in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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