• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Code Snippets / Python Hangman Game

Python Hangman Game

Author: PFB Staff Writer
Last Updated: December 28, 2022

In Python, we can create simple games very quickly. In this article, we will discuss the implementation of the Python Hangman Game.

Table of Contents
  1. How is Hangman Game Played?
  2. How to Create Hangman Game in Python?
  3. Python Code for Hangman Game
  4. Conclusion

How is Hangman Game Played?

The hangman game is a multiplayer game. In this game, one player selects a word. Other players have a certain number of guesses to guess the characters in the word. If the players are able to guess the characters in the entire word within certain attempts, they win. Otherwise, they lose.

How to Create Hangman Game in Python?

To create a hangman game in Python, we will use the following steps.

  • First, we will ask for the name of the user. We will take the user input using the input() method. After execution, the input() method takes the input from the user and returns a string.
  • Next, we will select a word and ask the user to start guessing the characters in the word.
  • We will also define the maximum number of attempts the user can take.
  • Now, we will use a while loop to repeatedly ask the user to guess the character until the attempts are exhausted.
  • Inside the while loop, if the user guesses the correct character. We will include it in the response. Otherwise, we will notify the user that they made a mistake.
  • If the user is able to guess all the characters of the word within the maximum number of attempts, they win the game.
  • If the user exhausts all their attempts before guessing the entire word, they lose.

Python Code for Hangman Game

Following is a Python script of the classic game “Hangman”. A row of dashes represents the word to guess. If the player guesses a letter in the word, the script writes it in all its correct positions.  The player has 10 turns to guess the word. You can easily customize the game by changing the variables.

Make sure that you understand what each line does. All the python statements have been explained using python comments for this reason.

#importing the time module import time #welcoming the user name = input("What is your name? ") print ("Hello, " + name, "Time to play hangman!") #wait for 1 second time.sleep(1) print ("Start guessing...") time.sleep(0.5) #here we set the secret. You can select any word to play with. word = ("secret") #creates an variable with an empty value guesses = '' #determine 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 ("You won") # exit the script break # 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 Lose" print ("You Lose" ) 

Output:

What is your name? Aditya Hello, Aditya Time to play hangman! Start guessing... ______guess a character:s s_____guess a character:e se__e_guess a character:c sec_e_guess a character:r secre_guess a character:e secre_guess a character:t secretYou won

Enjoy it!!

Conclusion

In this article, we have discussed the implementation of the hangman game in Python. To learn more about python programming, you can read this article on string manipulation. You might also like this article on python if else shorthand.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Code Snippets, Games, Scripts Author: PFB Staff Writer

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us