Hangman game - python

Welcome to my first blog post! Today, we're diving into the world of Python programming

Today python is the most popular programming language, there is no doubt everyone says Python is a beginner-friendly language it is easy to learn stuff like that, i have been code more than 3 years in Python. I will say yes python is very easy to understand. OK in this blog we’re going to build one of the good beginner-level projects Hangman game. this game is so simple, I believe we all played at a young age. ok, what is the Hangman Game? here is the answer to it

One player thinks of a word and draws dashes representing each letter. The other player guesses letters one at a time. If the guessed letter is in the word, it's revealed in its correct position(s); if not, a part of a hangman is drawn. The guessing player continues until they guess the word correctly or make too many incorrect guesses, resulting in the hangman being completed and the game being lost.












Before moving into this project we need some requirements:


1. Understanding Python basic 

         If you don’t know  Python basic concepts don’t worry I have attached the best Python learning               websites you can learn from that:

         w3 school:  https://www.w3schools.com/python/default.asp

         Geeks for Geeks: https://www.geeksforgeeks.org/python-programming-language/


2. Code editor ( IDLE / vs code / Pycharm)


ok. let's start to build our project. 



First, import the random library. the random library in Python provides a function for generating random numbers, selections 

here is how you can import the library:






once you import you need to create a list to store names or anything whatever you want in this example I will take sports name :











now I have created a list to store sports names i have assigned only 3 so we can easily understand.

once assigned we need to tell our system to pick any name in this sports list here is how you can do this






in this code, I have assigned one variable chosen word so we can store the name of what system choose

now that we have completed the important step of this project let's look at  other codes

in the next step, we wanna assign the lives the user has to find system chosen word

lives = 5      


now we're going to assign a variable display that starts empty. then it's filled with underscores. each underscore represents a letter in the word that needs to be guessed. so if the word is " football", the display will initially become ["_","_","_","_","_","_","_","_"].this helps in showing the player how many letters are there in the word they need to guess


display = []


for letter in range(len(chosen_word)):

    display += '_'



print(display)


now assign a loop where the player guesses the letter until they have no lives left

while lives > 0:

now we wanna take player input and convert it into lowercase ( important we only take one letter at a time)

    player_input = input("enter the guess
letter(int sports name):").lower()

 

next, assign another loop inside into the while loop  to iterate over the characters in the chosen word like this

while lives > 0:
   
    player_input = input("enter the guess
letter(int sports name):").lower()

    for i in range(len(chosen_word)):

'''

in this code we wanna check player input
letter matches with chosen sports name
if it is it updates the display with the
correct letter

'''

        if(player_input == chosen_word[i]):

            display[i] += chosen_word[i]
            print(display)



In case if player input does not match with the chosen word (sport name ) then we wanna decrease the lives of the player

if user_input not in chosen_word:

        lives -= 1


another condition we should check is if the display doesn't have an underscore ("_") which means the player found all the letters so we wanna print you win statement

elif '_' not in display:

        print("you win")
        break
#once the user wins this game we don't need to run loops again
#so we should have used a break statement

in the next step, we wanna check the user's chance which means lives become a 0 so while loop will stop running which means the user guessed the wrong letters 

if(lives == 0):

    print("you loss")



OK now we have completed this project here is the full code :

import random

sports=["cricket","football","badminton"]

chosen_word = random.choice(sports)


lives = 5      


display = []


for letter in range(len(chosen_word)):

    display += '_'



print(display)

while lives > 0:
   
    user_input = input("enter the guess
letter(int sport name):").lower()

    for i in range(len(chosen_word)):
        if(user_input == chosen_word[i]):

            display[i] += chosen_word[i]
            print(display)

   
    if user_input not in chosen_word:

        lives -= 1


    elif '_' not in display:

        print("you won")
        break



if(lives == 0):

    print("you loss")





Conclusion:

I hope you found this project easy to understand and fun to work on! If you have any questions or run into any problems, don't hesitate to reach out.  Happy coding 😊





Comments

Popular Posts