0% found this document useful (0 votes)
34 views10 pages

PWD MP

Python microproject

Uploaded by

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

PWD MP

Python microproject

Uploaded by

laibanaaz999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

INTRODUCTION

The origins of Hangman are obscure meaning not discovered,


but it seems to have arisen in Victorian times, ” says Tony
Augarde, author of The Oxford Guide to Word Games. The
game is mentioned in Alice Bertha Gomme’s “Traditional
Games” in 1894 under the name “Birds, Beasts and Fishes.” The
rules are simple; a player writes down the first and last letters of
a word and another player guesses the letters in between. In
other sources, [where?] the game is called “Gallows”, “The
Game of Hangin”, or “Hanger”.

Code
# Python Program to illustrate
# Hangman Game

Import random

From collections import Counter

someWords = ‘’’apple banana mango strawberry


orange grape pineapple apricot lemon coconut watermelon
cherry papaya berry peach lychee muskmelon’’’

someWords = someWords.split(‘ ‘)
# randomly choose a secret word from our “someWords” LIST.

Word = random.choice(someWords)

If __name__ == ‘__main__’:

Print(‘Guess the word! HINT: word is a name of a


fsomeWord
For I in word:

# For printing the empty spaces for letters of the word

Print(‘_’, end=’ ‘)

Print()

Playing = True

# list for storing the letters guessed by the player

letterGuessed = ‘’

chances = len(word) + 2
correct = 0

flag = 0

try:
while (chances != 0) and flag == 0: # flag is updated when
the word is correctly guessed

print()

chances -= 1

try:

guess = str(input(‘Enter a letter to guess: ‘))

except:

print(‘Enter only a letter!’)

continue

# Validation of the guess


If not guess.isalpha():

Print(‘Enter only a LETTER’)

Continue

Elif len(guess) & gt > 1:

Print(‘Enter only a SINGLE letter’)

Continue

Elif guess in letterGuessed:


Print(‘You have already guessed that letter’)

Continue

# If letter is guessed correctly


If guess in word:

# k stores the number of times the guessed letter


occurs in the word

K = word.count(guess)

For _ in range(k):

letterGuessed += guess # The guess letter is added


as many times as it occurs

# Print the word

For char in word:

If char in letterGuessed and (Counter(letterGuessed) !


= Counter(word)):

Print(char, end=’ ‘)
Correct += 1

# If user has guessed all the letters

# Once the correct word is guessed fully,

Elif (Counter(letterGuessed) == Counter(word)):

# the game ends, even if chances remain

Print(“The word is: “, end=’ ‘)

Print(word)

Flag = 1

Print(‘Congratulations, You won!’)

Break # To break out of the for loop


Break # To break out of the while loop

Else:
Print(‘_’, end=’ ‘)

# If user has used all of his chances

If chances & lt<= 0 and (Counter(letterGuessed) !=


Counter(word)):
Print()

Print(‘You lost! Try again..’)

Print(‘The word was {}’.format(word))

Except KeyboardInterrupt:

Print()
Print(‘Bye! Try again.’)

Exit()

Output

You might also like