0% found this document useful (0 votes)
20 views2 pages

Hangman Py Code

This document defines a hangman game function that: 1) Generates a random word from a list of words for the player to guess 2) Tracks the number of turns remaining and prints hangman art as turns are lost 3) Allows the player to guess letters and checks if they are in the word, revealing letters or decrementing turns if incorrect 4) Ends the game if the word is guessed or turns run out, printing the outcome

Uploaded by

shudeep2k6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views2 pages

Hangman Py Code

This document defines a hangman game function that: 1) Generates a random word from a list of words for the player to guess 2) Tracks the number of turns remaining and prints hangman art as turns are lost 3) Allows the player to guess letters and checks if they are in the word, revealing letters or decrementing turns if incorrect 4) Ends the game if the word is guessed or turns run out, printing the outcome

Uploaded by

shudeep2k6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 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