Project Report - Hangman
Project Report - Hangman
PROJECT REPORT
1
CERTIFICATE OF AUTHENTICITY
DATE: 27/11/22
PLACE: Bangalore
2
ACKNOWLEDGEMENT
Apart from my efforts, the success of my project is largely dependent on the
encouragement and guidelines of many others. I take this opportunity to
express my gratitude to the people who have been instrumental in the
successful completion of this project.
Last but not the least, I thank the management for giving me a chance to do
this wonderful project.
“Hangman is great, it teaches you that by saying wrong things, you could end
someone’s life”
- Anonymous
3
CONTENTS
TOPIC PAGE
NO.
1.INTRODUCTION
1.1 Python Programming Language 5
1.2 Applications of Python 7
2.SYSTEM REQUIREMENTS
2.1 Software Requirements 8
2.2 Hardware Requirements 8
3.HANGMAN
3.1 Introduction to Hangman 9
3.2 Flowchart 10
4.SOURCE CODE 11
5.OUTPUT 15
6.BIBLIOGRAPHY 18
4
INTRODUCTION
5
6. Free: Python is free to download and use. This means you can
download it for free and use it in your application.
6
1.2 Applications of Python programming language
Python can be used to develop different applications like web
applications, graphic user interface based applications, software
development application, scientific and numeric applications, network
programming, Games and 3D applications and other business
applications. It makes an interactive interface and easy development of
applications. There are many applications of Python, here are some of
them.
1. Web development: Web frameworks like Django and Flask are based
on Python. They help you write server side code which helps to
manage database, write backend programming logic, mapping URLs,
etc.
2. Machine learning: There are many machine learning applications
written in Python. Machine learning is a way to write a logic so that a
machine can learn and solve a particular problem on its own. For
example, products recommendation in websites like Amazon,
Flipkart, eBay etc. is a machine learning algorithm that recognises
user’s interest. Face recognition and Voice recognition in your phone
is another example of machine learning.
3. Data Analysis: Data analysis and data visualisation in form of charts
can also be developed using Python.
4. Scripting: Scripting is writing small programs to automate simple
tasks such as sending automated response emails, etc. Such types of
applications can also be written in Python programming language.
5. Game development: You can develop games using Python.
6. Desktop applications: You can develop desktop applications in
Python using libraries like TKinter or Q
7
SYSTEM REQUIREMENTS
8
HANGMAN
9
3.2 Flowchart
10
SOURCE CODE
3.1 hangMan.py
# Stages of the hanging man corresponding to number of tries
stages = ['',
R'''
____________
| |
| ◯
| \|/
| /‾\
|
|
''',
'''
____________
| |
| ◯
| \|/
| /
|
|
''',
'''
____________
| |
| ◯
| \|/
|
|
|
''',
'''
____________
| |
| ◯
| |/
|
|
|
''',
'''
____________
| |
| ◯
| |
|
|
|
''',
'''
____________
| |
| ◯
|
|
|
|
''',
'''
____________
| |
|
|
|
|
|
'''
11
3.2 wordLoader.py
import random
# Chooses a random word from the text file of the category of words.
def getWord(wordCat):
if wordCat == 1:
file = open("movies.txt")
elif wordCat == 2:
file = open("cars.txt")
else:
exit()
wordList = file.read().split("\n")
word = random.choice(wordList)
return word
word = getWord(wordCat)
# Converts the word into underscore form (except special symbols)
def wordBlanks(word):
wordDisplay = ''
for char in word:
if char.isalnum():
wordDisplay += '_'
else:
wordDisplay += char
return wordDisplay
12
3.3 game.py
import wordLoader
from hangMan import stages
def gameLogic():
word = wordLoader.word.upper()
wordCompletion = wordLoader.wordBlanks(wordLoader.word)
guessed = False
guessedLetters = []
guessedWords = []
tries = 6
print("You have", tries, "tries left")
print(wordCompletion)
print(stages[tries])
print("\n")
while not guessed and tries > 0:
guess = input("Please guess a letter or a word: ").upper()
if len(guess) == 1 and guess.isalnum():
if guess in guessedLetters:
print("You already guessed the letter", guess)
elif guess not in word:
print(guess, "is not in the word")
tries -= 1
guessedLetters.append(guess)
else:
print("Nice!", guess, "is in the word")
guessedLetters.append(guess)
wordAsList = list(wordCompletion)
indices = [i for i, letter in enumerate(word) if letter ==
guess]
for index in indices:
wordAsList[index] = guess
wordCompletion = ''.join(wordAsList)
if '_' not in wordCompletion:
guessed = True
elif len(guess) == len(word):
if guess in guessedWords:
print("You have already guessed this word")
elif guess != word:
print(guess, "is not the word")
tries -= 1
guessedWords.append(guess)
else:
guessed = True
wordCompletion = word
else:
print("Not a valid input")
if not guessed:
print("You now have", tries, "tries left")
if tries == 0:
print("The word was", word)
else:
print(wordCompletion)
print(stages[tries])
print("\n")
if guessed:
print("You Win")
13
3.4 main.py
import game
import wordLoader
import hangMan
def main():
game.gameLogic()
while input("Do you want to keep playing? (Y/N)").upper() == "Y":
wordCat = int(input("Welcome!\nPlease select the category of words
you want to play.\n1. Movies\n2. Cars\n"))
if wordCat not in range(1, 3):
exit()
wordLoader.word = wordLoader.getWord(wordCat)
game.gameLogic()
if __name__ == "__main__":
main()
14
OUTPUT
15
Identifies wrong letters
Draws the body of the Hangman
16
Prompts to replay the game upon completion.
17
Bibliography
1. Computer Science with Python – Class XI by Sumita Arora
2. Wikipedia.org
18