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 966 2
Number Guessing Game using Python 656 2
Pyperclip: Not Implemented Error 957 2
Hangman Game using Python 16193 2
Using Databases with CherryPy application 1594 2
nose: Working 488 2
pytest: Working 490 2
Open Source and Hacktoberfest 851 2
Managing Logs of CherryPy applications 949 2
Top 20 Data Science Tools 654 2
Ajax application using CherryPy 778 2
REST application using CherryPy 639 2
On Screen Keyboard using Python 5085 2
Elastic Net Regression 778 2
US Presidential Election 2020 Prediction using Python 776 2
Sound Source Separation 1131 2
URLs with Parameters in CherryPy 1547 2
Testing CherryPy application 603 2
Handling HTML Forms with CherryPy 1364 2
Applications of Natural Language Processing in Businesses 489 2
NetworkX: Multigraphs 616 2
Tracking User Activity with CherryPy 1308 2
CherryPy: Handling Cookies 767 2
Introduction to NetworkX 613 2
TorchServe - Serving PyTorch Models 1239 2
Fake News Detection Model using Python 701 2
Keeping Home Routers secure while working remotely 462 2
Email Slicer using Python 2947 2
NetworkX: Creating a Graph 1078 2
Best Mathematics Courses for Machine Learning 530 2
Hello World in CherryPy 643 2
Building dependencies as Meson subprojects 915 2
Vehicle Detection System 1052 2
NetworkX: Examining and Removing Graph Elements 575 2
Handling URLs with CherryPy 515 2
PEP 8 - Guide to Beautiful Python Code 736 2
NetworkX: Drawing Graphs 588 2
Mad Libs Game using Python 617 2
Hosting Cherry applications 588 2
Top 5 Free Online IDEs of 2020 839 2
pytest: Introduction 512 2
Preventing Pwned and Reused Passwords 559 2
Contact Book using Python 2058 2
Introduction to CherryPy 521 2
nose: Introduction 487 2
Text-based Adventure Game using Python 2900 2
NetworkX: Adding Attributes 2104 2
NetworkX: Directed Graphs 995 2
Dice Simulator using Python 539 2
Decorating CherryPy applications using CSS 769 2

Comments