Hang Man In Python

Hang Man In Python

ยท

4 min read

Hang Man In Python

Hey there! ๐Ÿค— In this tutorial, I will walk you through building the classic word-guessing game, "Hangman", using Python. Let's get started with the code!

First, we import the random module to randomly select a word from a list of words. Here's the code:

import random

# define a list of words to choose from
word_list = ["python", "programming", "computer", "algorithm", "software", "developer"]

# select a random word from the list
secret_word = random.choice(word_list)

We create a list of words to choose from and assign one of them to the variable secret_word using the random.choice() function.

Next, we initialize two more variables: a list to store the letters guessed by the user, and a variable to store the number of guesses remaining. Here's the code:

# create a list to store the letters guessed by the user
guessed_letters = []

# create a variable to store the number of guesses remaining
num_guesses = 6

We create an empty list called guessed_letters to store the letters guessed by the user. The num_guesses variable is set to 6, which means the user will have 6 chances to guess the word.

Now, we start a loop to prompt the user to guess letters. Here's the code:

# start a loop to prompt the user to guess letters
while num_guesses > 0:
    # print the current state of the word
    for letter in secret_word:
        if letter in guessed_letters:
            print(letter, end=" ")
        else:
            print("_", end=" ")
    print()

    # prompt the user to enter a guess
    guess = input("Enter a letter: ")

The loop will continue until the user runs out of guesses or correctly guesses the word. Inside the loop, we use a for loop to print the current state of the word. We check if each letter in the secret_word is in the guessed_letters list, and if so, we print the letter. If not, we print an underscore to represent a letter that hasn't been guessed yet. We then prompt the user to enter a guess using the input() function.

We need to check if the guess is valid, i.e., if it is a single letter. Here's the code:

    # check if the guess is valid
    if len(guess) != 1 or not guess.isalpha():
        print("Invalid guess. Please enter a single letter.")
        continue

We use an if statement to check if the length of the guess is not 1 or if the guess is not an alphabetical letter. If either condition is true, we print an error message and use the continue statement to go back to the start of the loop and prompt the user to enter a new guess.

Next, we check if the guess is correct. Here's the code:

    # check if the guess is correct
    if guess in secret_word:
        print("Correct!")
        guessed_letters.append(guess)
    else:
        print("Incorrect.")
        num_guesses -= 1

We use another if statement to check if the guess is in the secret_word. If it is, we print a message telling the user that they guessed correctly and add the letter to the guessed_letters list. If the guess is incorrect, we print a message telling the user that they guessed incorrectly and subtract 1 from the num_guesses variable.

Finally, we check if the user has guessed all the letters in the word. Here's the code:

    # check if the user has guessed all the letters
    if set(guessed_letters) == set(secret_word):
        print("Congratulations, you guessed the word!")
        break

We use a set to check if the letters in the guessed_letters list are the same as the letters in the secret_word. If they are the same, we print a message telling the user that they guessed the word and use the break statement to exit the loop.

If the user runs out of guesses, we print the solution. Here's the code:

# if the user runs out of guesses, print the solution
if num_guesses == 0:
    print("You ran out of guesses. The word was:", secret_word)

We use an if statement to check if the num_guesses variable is equal to 0. If it is, we print a message telling the user that they ran out of guesses and print the secret_word.

That's it! ๐ŸŽ‰ We've successfully built the "Hangman" game using Python. I hope this tutorial was helpful and easy to follow. Have fun playing "Hangman" with your friends and family!๐Ÿ‘

Did you find this article valuable?

Support Neural Nonsense by becoming a sponsor. Any amount is appreciated!

ย