Aakash Verma
4 min readAug 24, 2019

Word Guessing Game in Python

Hi Reader ! It’s been a long time since my last visit into Writing . I have been busy doing rigorous coding last two months both in FreeCodeCamp and HackerRank , Both of which are fairly good enough sites to enhance your coding skills and take it to the level which makes you fully satisfied with your work( as i believe) . Also when language is python it works as a pizza with extra cheese . I love Python and way it works .If I Start talking about it , this article will be very lengthy to read . So not wasting let’s come directly to the point . Today I’m gonna make a word Guessing Game which makes use of Python random Library and prompts user to guess a word by guessing each letter at a time ,Isn’t it amazing .So let’s start this …

Python : A high level , Object Oriented Programming Language ,dynamically typed, Beginner Friendly Language with simple syntax which let’s you focus more on problem solving rather worrying about the language Syntax.

So Here Is the code For the Word Guessing Game :

import random#list of words
words = ["lemon","mango","banana","apple"]
#ask Name
name=input("Enter Your name")
print(f"Hi {name} , Let's start the game")
print("\n\n\n\t\t ***Word Guessing Game*** ")
print("\n You have 10 attempts to guess the word correctly .")
#list to store wrong guessed_letters
wrong_list=[]
#generate random word
original_word=random.choice(words)
print(f"The word is of {len(original_word)} letters. ")#create an empty list
guessed_word = []
for i in range(len(original_word)):
guessed_word.append("__")
print(*([i for i in guessed_word]))
c=8
while(c):
c=c-1
#take input from user
guessed_letter = input("Guess the letter")
#check if guessed_letter is an alphabet
if not guessed_letter.isalpha():
print('Guess only a letter')
#check if guessed letter length is one or not
elif(len(guessed_letter)>1):
print('Guess only one letter...')
#check that letter chosen by user is already guessed or not
elif(guessed_letter in wrong_list):
print('You have Already guessed this letter')
#check if guessed_letter is matches with original_word
if guessed_letter in original_word:
for i in range(len(original_word)):
if original_word[i] == guessed_letter:
guessed_word[i]=original_word[i]
else:
"""if guessed_letter is not in original_word
prompt user for wrong chosen letter"""
print("You Guessed wrong letter")
wrong_list.append(guessed_letter)
guess_word = [i for i in guessed_word]
guess_word = "".join(guess_word)
if original_word ==guess_word :
print('YAY !!, You have Got the letter ...')
exit(0)
#print the guessed word
print(*([i for i in guessed_word]))
#prompt user showing number of attempts left to win
print(f"You have {c} attempts left")
if c==0:
if original_word!=guessed_word :
print(f"You lost the game ,The original Word was {original_word}")
exit(0)

Python’s random library allows you to randomly choose a item from a list of item . There are many in-built methods available with this library but we will work with random.choice(). Basically it Return a random element from the non-empty sequence seq. If sequence is empty it raises Index_Error.

Let’s do a line by line interpretation of what’s going on in the code :

First we import python random library and then create a list containing list of words user need to guess.After that we Prompt the Player for his/her name .After greeting user , we start the game ,the player has 8 attempts to guess the correct word and if player fails to guess the original word in 8 attempts than he is prompted with original word and a loser message .

With the help of random library in python we choose a random word from our words list and then create an empty list of same length containing “__” in them to display user the word according to his guesses of letter included in them , for this a while loop can be used .Then user is prompted to take his guess and player’s input is saved in guessed_letter variable .

We make a check if player’s input is an alphabet character and also it is of length 1 character and if player’s input contain the letter that is already been guessed by the player than player is prompted with the message respectively

We check that whether character entered by player is in our original word.If Guessed letter is in original word we display the guessed_word including that letter and rest of places filled with “__” .This is done 8 times in total if player is able to make a correct letter guess than he is acknowledged with his success message else a failure message with original message come up on the screen .

So that’s it for now, Try to run above code in your setup and let me know if you like it . . . .

Aakash Verma

Computer Science Undergrad ,Tech Enthusiast , Loves writing about Technology