Hangman Game using Python














































Hangman Game using Python



Hangman Game using Python

Hangman is a guessing game for two or more players. One player thinks of a word and the other(s) tries to guess it by suggesting letters within a certain number of guesses.




We will be making a similar Hangman Game using Python. We will be using fruits' names for words to guess.


The word to guess will be chosen from the list of words at random using 'random.choice()' method.


Save the below code in a file named 'hangman.py'.


import random hang = [""" H A N G M A N - Fruit Edition +---+ | | | | | | =========""", """ H A N G M A N - Fruits Edition +---+ | | O | | | | =========""", """ H A N G M A N - Fruits Edition +---+ | | O | | | | | =========""", """ H A N G M A N - Fruits Edition +---+ | | O | /| | | | =========""", """ H A N G M A N - Fruits Edition +---+ | | O | /|\ | | | =========""", """ H A N G M A N - Fruits Edition +---+ | | O | /|\ | / | | =========""", """ H A N G M A N - Fruits Edition +---+ | | O | /|\ | / \ | | ========="""] def getRandomWord(): words = ['apple', 'banana', 'mango', 'strawberry', 'orange', 'grape', 'pineapple', 'apricot', 'lemon', 'coconut', 'watermelon', 'cherry', 'papaya', 'berry', 'peach', 'lychee', 'muskmelon'] word = random.choice(words) return word def displayBoard(hang, missedLetters, correctLetters, secretWord): print(hang[len(missedLetters)]) print() print('Missed Letters:', end=' ') for letter in missedLetters: print(letter, end=' ') print("\n") blanks = '_' * len(secretWord) for i in range(len(secretWord)): # replace blanks with correctly guessed letters if secretWord[i] in correctLetters: blanks = blanks[:i] + secretWord[i] + blanks[i+1:] for letter in blanks: # show the secret word with spaces in between each letter print(letter, end=' ') print("\n") def getGuess(alreadyGuessed): while True: guess = input('Guess a letter: ') guess = guess.lower() if len(guess) != 1: print('Please enter a single letter.') elif guess in alreadyGuessed: print('You have already guessed that letter. Choose again.') elif guess not in 'abcdefghijklmnopqrstuvwxyz': print('Please enter a LETTER.') else: return guess def playAgain(): return input("\nDo you want to play again? ").lower().startswith('y') missedLetters = '' correctLetters = '' secretWord = getRandomWord() gameIsDone = False while True: displayBoard(hang, missedLetters, correctLetters, secretWord) guess = getGuess(missedLetters + correctLetters) if guess in secretWord: correctLetters = correctLetters + guess foundAllLetters = True for i in range(len(secretWord)): if secretWord[i] not in correctLetters: foundAllLetters = False break if foundAllLetters: print('\nYes! The secret word is "' + secretWord + '"! You have won!') gameIsDone = True else: missedLetters = missedLetters + guess if len(missedLetters) == len(hang) - 1: displayBoard(hang, missedLetters, correctLetters, secretWord) print('You have run out of guesses!\nAfter ' + str(len(missedLetters)) + ' missed guesses and ' + str(len(correctLetters)) + ' correct guesses, the word was "' + secretWord + '"') gameIsDone = True if gameIsDone: if playAgain(): missedLetters = '' correctLetters = '' gameIsDone = False secretWord = getRandomWord() else: break


To run the game, enter the below command in a Terminal/Command Line:


python3 hangman.py


A sample output will look like this


H A N G M A N - Fruit Edition +---+ | | | | | | ========= Missed Letters: _ _ _ _ _ _ Guess a letter: a H A N G M A N - Fruits Edition +---+ | | O | | | | ========= Missed Letters: a _ _ _ _ _ _ Guess a letter: b H A N G M A N - Fruits Edition +---+ | | O | | | | | ========= Missed Letters: a b _ _ _ _ _ _ Guess a letter: c H A N G M A N - Fruits Edition +---+ | | O | | | | | ========= Missed Letters: a b c _ _ _ _ _ Guess a letter: h H A N G M A N - Fruits Edition +---+ | | O | | | | | ========= Missed Letters: a b c h _ _ _ _ Guess a letter: e H A N G M A N - Fruits Edition +---+ | | O | | | | | ========= Missed Letters: a b c h e _ _ _ Guess a letter: r H A N G M A N - Fruits Edition +---+ | | O | | | | | ========= Missed Letters: a b c h e r r _ Guess a letter: y Yes! The secret word is "cherry"! You have won! Do you want to play again? no



More Articles of Aniket Sharma:

Name Views Likes
Pyperclip: Installation and Working 1105 2
Number Guessing Game using Python 754 2
Pyperclip: Not Implemented Error 1446 2
Hangman Game using Python 19342 2
Using Databases with CherryPy application 2174 2
nose: Working 587 2
pytest: Working 576 2
Open Source and Hacktoberfest 948 2
Managing Logs of CherryPy applications 1133 2
Top 20 Data Science Tools 762 2
Ajax application using CherryPy 909 2
REST application using CherryPy 753 2
On Screen Keyboard using Python 7656 2
Elastic Net Regression 960 2
US Presidential Election 2020 Prediction using Python 860 2
Sound Source Separation 1324 2
URLs with Parameters in CherryPy 2140 2
Testing CherryPy application 747 2
Handling HTML Forms with CherryPy 1787 2
Applications of Natural Language Processing in Businesses 588 2
NetworkX: Multigraphs 813 2
Tracking User Activity with CherryPy 1618 2
CherryPy: Handling Cookies 1139 2
Introduction to NetworkX 693 2
TorchServe - Serving PyTorch Models 1503 2
Fake News Detection Model using Python 809 2
Keeping Home Routers secure while working remotely 553 2
Email Slicer using Python 3123 2
NetworkX: Creating a Graph 1185 2
Best Mathematics Courses for Machine Learning 622 2
Hello World in CherryPy 909 2
Building dependencies as Meson subprojects 1200 2
Vehicle Detection System 1200 2
NetworkX: Examining and Removing Graph Elements 685 2
Handling URLs with CherryPy 610 2
PEP 8 - Guide to Beautiful Python Code 829 2
NetworkX: Drawing Graphs 726 2
Mad Libs Game using Python 740 2
Hosting Cherry applications 693 2
Top 5 Free Online IDEs of 2020 974 2
pytest: Introduction 600 2
Preventing Pwned and Reused Passwords 644 2
Contact Book using Python 2288 2
Introduction to CherryPy 616 2
nose: Introduction 554 2
Text-based Adventure Game using Python 3382 2
NetworkX: Adding Attributes 3037 2
NetworkX: Directed Graphs 1193 2
Dice Simulator using Python 640 2
Decorating CherryPy applications using CSS 995 2

Comments