Hangman Game in Python
The origins of Hangman are obscure meaning not discovered, but it seems to have arisen in Victorian times, ” says Tony Augarde, author of The Oxford Guide to Word Games. The game is mentioned in Alice Bertha Gomme’s “Traditional Games” in 1894 under the name “Birds, Beasts and Fishes.” The rules are simple; a player writes down the first and last letters of a word and another player guesses the letters in between. In other sources, [where?] the game is called “Gallows”, “The Game of Hangin”, or “Hanger”.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
This is a simple Hangman game using Python programming language. Beginners can use this as a small project to boost their programming skills and understanding logic.
- The Hangman program randomly selects a secret word from a list of secret words. The random module will provide this ability, so line 1 in program imports it.
- The Game: Here, a random word (a fruit name) is picked up from our collection and the player gets limited chances to win the game.
- When a letter in that word is guessed correctly, that letter position in the word is made visible. In this way, all letters of the word are to be guessed before all the chances are over.
- For convenience, we have given length of word + 2 chances. For example, word to be guessed is mango, then user gets 5 + 2 = 7 chances, as mango is a five letter word.
Note: Please run the program on your terminal.
omkarpathak@omkarpathak-Inspiron-3542:~/Documents/ Python-Programs$ python P37_HangmanGame.py Guess the word! HINT: word is a name of a fruit _ _ _ _ _ Enter a letter to guess: m _ _ m _ _ Enter a letter to guess: o _ _ m o _ Enter a letter to guess: l l _ m o _ Enter a letter to guess: e l e m o _ Enter a letter to guess: n l e m o n Congratulations, You won!
Try it yourself Exercises:
- You can further enhance program by adding timer after every Guess
- You can also give hints from the beginning to make the task a bit easier for user
- You can also decrease the chance by one only if player’s guess is WRONG. If the guess is right,
player’s chance is not reduced
This article is contributed by Omkar Pathak. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.