Hangman Py Code

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

import random

def hangman():

list_of_words=['hardwork','smartwork','computer','string','literal','string','tuple
','dictionary']
word=random.choice(list_of_words)
turns = 10
guessmade=''
valid_entry=set('abcdefghijklmnopqrstuvwxyz')

while len(word)>0:
main_word=''
missed=0

for letter in word:


if letter in guessmade:
main_word = main_word+letter
else:
main_word = main_word +'_ '

if main_word == word:
print(main_word)
print('you won')
break

print("guess the word ",main_word)


guess = input()

if guess in valid_entry:
guessmade=guessmade+guess
else:
print('enter vaild character ')
guess=input()

if guess not in word:


turns=turns-1

if turns==9:
print('you have 9 more turns left')
print('--------------------------')
if turns==8:
print('you have 8 more turns left')
print('--------------------------')
print(' o ')
if turns==7:
print('you have 7 more turns left')
print('--------------------------')
print(' o ')
print(' | ')
if turns==6:
print('you have 6 more turns left')
print('--------------------------')
print(' o ')
print(' | ')
print(' / ')
if turns==5:
print('you have 5 more turns left')
print('--------------------------')
print(' o ')
print(' | ')
print(' / \ ')
if turns==4:
print('you have 4 more turns left')
print('--------------------------')
print(' o ')
print(' |- ')
print(' / \ ')
if turns==3:
print('you have 3 more turns left')
print('--------------------------')
print(' o ')
print(' -|- ')
print(' / \ ')
if turns==2:
print('you have 2 more turns left')
print('--------------------------')
print(' o | ')
print(' -|- ')
print(' / \ ')
if turns==1:
print('last chance !')
print('--------------------------')
print(' o_| ')
print(' -|- ')
print(' / \ ')
if turns==0:
print('GAME OVER')
print('you losse')
print('The Word is : ',word)

break

Name = input("player name -> ")

print("Goodluck",Name,"!!")
print("------------------")
print("try to guess the right word in less than 10 attempts ")
hangman()

You might also like