Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Hangman Game

Python Project

Kumkum K (1BI22IS051)
Kusuma A (1BI22IS052)
Introduction
 In the hangman game, one player thinks of a word and the other players have to
guess the word letter by letter. Whenever the guessed letter is correct, the
player who decided on the word will indicate the position of the letter.
 The logic of creating the Hangman game with python is that we will have users
who will guess a letter and all users will have a very limited number of guesses.
 So to code this game with Python, you can create a wordlist from which users
can think of the words. In the process, we will also need some helper functions
to check if the user entered a single letter or if the letter entered by the user is
in the hidden word.
 To build the Hangman game with the Python programming language, you need
to use basic Python concepts like the random module, integers, strings,
characters, input and output operations, and Boolean values.
Python Concepts
 The `random` module in Python provides functions to generate random
numbers and make random choices. One of the most commonly used functions
in the `random` module is `choice()`.
 The syntax for using the `choice()` function is as follows:
import random
random.choice(sequence)
 Here, `sequence` can be any iterable object such as a list, string, or tuple. The
`choice()` function randomly selects an element from the given sequence and
returns it.
 Python def keyword is used to define
a function, it is placed before a function
name that is provided by the user to
create a user-defined function
 List is a sequence data type which is used to store the collection of data,
enclosed in [ ] and separated by commas. The syntax for creating list is :
ListName = [ListItem, ListItem1, ListItem2, ListItem3, ...]
Functions Used:
 print() - Output data to the screen.
 input() – Takes the input from the user and converts it into a string.
Explicitly convert it into an integer/float data type using
typecasting.
 len() – Returns the number of elements in a list and number of
characters in a string.
 append() - Adds an element to the end of a list.
 list() – Takes an iterable construct and turns it into a list.
 lower() – Converts a string or character to lowercase.
 join() – Joins the elements of a string iterable (e.g., list, tuple) with a
specified string as a separator.
Continue Statement:
Continue statement is a loop control statement that forces to execute
the next iteration of the loop while skipping the rest of the code inside
the loop for the current iteration only, i.e. When the continue statement
is executed in the loop, the code inside the loop following the continue
statement will be skipped for the current iteration and the next
iteration of the loop will begin.
Source Code
import random
def hangman():
words = [‘apple’, ‘banana’, ‘cherry’, ‘dragonfruit’,
‘elderberry’, ‘fig’, ‘grapefruit’, ‘honeydew’] # List of words to choose from
word = random.choice(words) # Randomly select a word from the list
word_letters = list(word) # Convert the word to a list of characters
guessed_letters = [] # Track the guessed letters
progress = ‘_’ * len(word) # Track the current progress of the word
attempts = 6 # Number of attempts
print(‘Welcome to Hangman!’)
while attempts > 0 and ‘_’ in progress:
print(‘\nWord:’, ‘ ‘.join(progress))
print(‘Attempts left:’, attempts)
guess = input(‘Enter a letter: ‘).lower()
if len(guess) != 1: # Check if the guess is a single letter
print(‘Please enter only a single letter.’)
continue
if guess in guessed_letters: # Check if the guess has already been made
print(‘You have already guessed that letter.’)
continue
guessed_letters.append(guess)

if guess in word_letters: # Check if the guess is in the word


for i in range(len(word_letters)):
if word_letters[i] == guess:
progress = progress[:i] + guess + progress[i+1:]
else:
print(‘Wrong guess!’)
attempts -= 1

if ‘_’ not in progress:


print(‘Congratulations! You guessed the word:’, word)
else:
print(‘Game over! The word was:’, word)
hangman()
Output
1) 2)

You might also like