Hangman in Python
Introduction
In this Python code snippet, we are going to implement an easy hangman solver using simple source code. The game code is adapted to work with both Python 2.x and Python 3.x. The example provided is a console based application for beginners. Let us get started with the tutorial…
Python 2.x program
There is no major difference between Python 2.x and Python 3.x code, however we had to modify the code a bit as strings are handled differently depending on Python version. If you are curious, at the end of this article I will provide few links for dealing with strings in different versions of Python.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# Import operating system module # This is needed to clear the screen import os # Import random module # This is needed to generate random strings import random # Clear console screen os.system('clear') # Enter player name name = raw_input("INPUT : Enter your name: ") # To quit the game, just type quit print("INFO : Welcome " + name + " to hangman") print("INFO : To exit type quit") # Pick a random word from list word = random.choice(['red', 'elephant', 'london', 'microsoft', 'david']) # Initialize guessed word with dashes # We use a byte array so that we can modify it (mutable) guessed = bytearray() for i in range(len(word)): guessed = guessed + b'-' # Number of attempts attempts = 0 # Game loop, you can change the number of attempts while attempts < 10: # Print current game status print("INFO : Word to guess " + guessed) print("INFO : You have " + str(10-attempts) + " attempts left") # Increment number of attempts attempts += 1 # Enter a character ch = raw_input("INPUT : type a character : ") # Quit game if ch == "quit": print("INFO : Good bye...") break # Search for the entered character in the secret word # We use a list comprehension. It returns the index # locations of the entered character if they are found indices = [i for i, x in enumerate(word) if x == ch] # Update the secret word with the guessed character for index, val in enumerate(guessed): if index in indices: guessed[index] = ch # Player won if guessed == word: print("INFO : You won") break # Player lost if attempts == 10 and guessed != word: print("INFO : You lost") |
Python 3.x program
In Python 3.x, strings are unicode by default so we need to take that into account. For more information about handling strings in Unicode, please check the references section.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# -*- coding: utf-8 -*- # Import operating system module import os # Import random module import random # Clear console screen os.system('clear') # Enter player name name = input("INPUT : Enter your name: ") # To quit the game just type quit print("INFO : Welcome " + name + " to hangman") print("INFO : To exit type quit") # Pick a random word from list word = random.choice(['red', 'elephant', 'london', 'microsoft', 'david']) # Initialize guessed word with dashes # We use a byte array because we can # modify it (mutable) guessed = bytearray() for i in range(len(word)): guessed = guessed + b'-' # Number of attempts attempts = 0 # Game loop goes for ever unless quit is entered while attempts < 10: print("INFO : Word to guess " + str(guessed)) print("INFO : You have " + str(10-attempts) + " attempts left") # Increment number of attempts attempts += 1 # Enter a character ch = input("INPUT : type a character : ") # Quit game if ch == "quit": print("INFO : Good bye...") break # Search for the character in the word # This is a list comprehension which # returns the index locations of the # entered character within the word that # we are tyring to guess indices = [i for i, x in enumerate(word) if x == ch] # Update the secret word with the guessed character for index, val in enumerate(guessed): if index in indices: guessed[index] = ord(ch) # Player won if guessed == word.encode('utf-8'): print("INFO : You won") break # Player lost if attempts == 10 and guessed != word.encode('utf-8'): print("INFO : You lost") |
That is all for today.
References
For help or questions? please use the comments section below…