A Simple Hangman Game Implemented In 3 Lines Of Python

hangman game in three lines of pythonShort Bytes: Today I’m sharing a clever implementation of Hangman in python by programmer Danver Braganza. Take a look at this 3-lines-long program and try to make it more compact.

You might have come across Python scripts teaching you how to code the classic game “Hangman”. For those who don’t know, it’s a word guessing game for two or more players. In the game, one player thinks of a word or a phrase and the other player tries to suggest letters or numbers within some fixed number of guesses.

When I came across a neat, small, working, fun implementation of Hangman on Hacker News, I decided to share it with fossBytes readers as it’s one of my favorite word games. Coder Danver Braganza decided to address the standard limit of 80 characters a line and write his hangman program in Python.

Also read: A Neural Network in 11 Lines of Python

“The restriction I set for myself was that each line must be just one Python statement or expression,” he writes. You can find the code below:

This 3 lines of Python code is a pretty compact implementation of Hangman. You can copy the above code, paste into hangman.py and run it.

On his website, Danver also mentions some particular features of the game:

  • A word is chosen randomly from the system dictionary. Of course, this dictionary includes proper names. The word list can be easily changed.
  • Keeping track of the number of (wrong) guesses left. Successful guesses do not add to the hangman, and repeated missed guesses do not add to the hangman.
  • Positional blanks hint at the hidden word, that are filled in with correct guesses.
  • An ASCII Hangman built as you continue to guess wrongly.
  • Multiple guesses are allowed, and non-alphabet character guesses are ignored.

You can read more about this Hangman implementation in 3 lines of python on Danver’s website.

If you can make this code more compact, share your suggestions in the comments below.

Get pure python hacker bundle at fossBytes store.

You may also like...

Latest Deals from fossBytes...All deals

    fossBytes Newsletter

    Get the latest technology news delivered to your inbox.
    SUBSCRIBE TO FOSSBYTES