INDIRAPURAM PUBLIC SCHOOL
C-Block, Sector 12, Pratap Vihar, Ghaziabad, Uttar Pradesh 201009
PROJECT
ON
HANGMAN GAME
Subject Name : Computer Science
Class & Section : XII - A
Session : 2019 - 2020
Submitted By: Submitted To:
Name : ARYAN SHARMA MS. NEHA PRAJAPATI
PGT- COMPUTER
Roll No : SCIENCE
__________________________ IPS, PRATAP VIHAR
Table of Contents
S.No. TOPICS Page No.
1. Acknowledgement
2. Certificate
3. Introduction
4. Code
5. Output
6.
7.
8.
9.
10.
Acknowledgement
I would like to express my special
thanks of gratitude to my teacher
Ms. Neha Prajapati as well as our
principal Mrs. Renu Sharma who
gave me the golden opportunity to
do this wonderful project on the
topic “Hangman game using
python”, which also helped me in
doing a lot of Research and I came
to know about so many new things
I am really thankful to them.
Secondly I would also like to thank
my parents and friends who helped
me a lot in finalizing this project
within the limited time frame.
Indirapuram Public School
Certificate
This is to certify that _________________________ of
class 12____ roll no__________ has completed his
investigatory project
entitled____________________________________________
_________in computers during the academic year
_______ for the partial fulfilment of his academic
course in the subject of Computer Science.
Sign of sign of
principal Subject teacher
_________
sign of examiner
Hangman Game using python
Hangman Game is a letter guessing game.
In the game of Hangman, a clue word is given by the program that the player has to
guess, letter by letter. The player guesses one letter at a time until the entire word has
been guessed. (In the actual game, the player can only guess 6 letters incorrectly before
losing).
Let’s say the word the player has to guess is “EVAPORATE”. For this exercise, write the
logic that asks a player to guess a letter and displays letters in the clue word that were
guessed correctly. For now, let the player guess an infinite number of times until they get
the entire word. As a bonus, keep track of the letters the player guessed and display a
different message if the player tries to guess that letter again. Remember to stop the
game when all the letters have been guessed correctly! Don’t worry about choosing a
word randomly or keeping track of the number of guesses the player has remaining – we
will deal with those in a future exercise.
An example of interaction can look like this:
>>> Welcome to Hangman!
_ _ _ _ _ _ _ _ _
>>> Guess your letter: S
Incorrect!
>>> Guess your letter: E
E _ _ _ _ _ _ _ E
...
And so on, until the player gets the word. Here is the source code to do the same in
Python
Code:
import os
import pickle
import time
width = "150"
height = "50"
os.system("mode con cols="+width+"lines="+height)
#welcoming the user
name = input("What is your name? ")
print ("Hello", name, "Time to play hangman!")
print ("")
#wait for 1 second
time.sleep(1)
print ("Start guessing...")
time.sleep(0.5)
#here we set the secret
word = "pollution"
#creates a variable with an empty value
guesses = ''
#determines the number of turns
turns = 10
# Create a while loop
#check if the turns are more than zero
while turns > 0:
# make a counter that starts with zero
failed = 0
# for every character in secret_word
for char in word:
# see if the character is in the players guess
if char in guesses:
# print then out the character
print (char,end="")
else:
# if not found, print a dash
print ("_",end="")
# and increase the failed counter with one
failed += 1
# if failed is equal to zero
# print You Won
if failed == 0:
print ('congrats!!!!',name,"You won")
# exit the script
break
print
# ask the user go guess a character
guess = input("guess a character:")
# set the players guess to guesses
guesses += guess
# if the guess is not found in the secret word
if guess not in word:
# turns counter decreases with 1 (now 9)
turns -= 1
# print wrong
print ("Wrong")
# how many turns are left
print ("You have",turns, 'more guesses')
# if the turns are equal to zero
if turns == 0:
# print "You Loose"
print ("You Loose")
OUTPUT: