Easy Python Program Example: Word Guessing Game

Day 15 in #100DaysOfCode

This is a very common example to execute when learning Python for the very first time. Many learners probably have tried this game once but let us see how it works once again.

We'll construct a word guessing game with the random module. This game is designed for novices learning to write in Python and provides a quick overview of strings, loops, and conditional (If, else) expressions.

The Python Random module is a built-in Python module that is used to create random integers. Because these are pseudo-random numbers, they are not actually random. This module may be used to generate random numbers, print a random value from a list or string, and so on.

Let us start coding!

First, we should import the "random module" with: import random

This is the library that we use in order to choose random words from a list of words

Then we are going to ask the user what is their name to address them with their name.

import random

name = input("What is your name? ")
print("Good Luck ", name, "!")

If the character doesn’t match the word that has been typed then “Wrong” will be given as output.

Whole code looks like this:

import random

# library that we use in order to choose
# on random words from a list of words

name = input("What is your name? ")

print("Good Luck ", name, "!")

repeat = True
while repeat == True :
    fruits =  ['apple', 'olive', 'tomato', 'melon', 'litchi',
    'mango', 'lime', 'kiwi', 'grapes', 'cherry',
    'banana', 'apricot', 'cucumber', 'guava', 'mulberry',
    'orange', 'papaya', 'pear', 'peach', 'berry']

    animals = ['ants', 'hippo', 'panda', 'giraffe', 'bat', 'bear',
    'catfish', 'cheetah', 'lizard', 'wolf', 'zebra', 'eagle',
    'cobra', 'goose', 'penguin', ]

    accessories = ['ring', 'bangle', 'lipstick',
    'jacket', 'boots', 'socks', 'stocking', 'muffler',
    'gloves', 'umbrella', 'ribbon']

    stationary = ['notebook', 'tape', 'pencil', 'eraser', 'sharpener',
    'files', 'fevicol', 'inkpot', 'chalk', 'duster',
    'glue', 'paper', 'cutter', 'chart', 'colours',
    'stapler', 'marker', 'staples', 'clips', 'calculator',
    'envelope', 'register']

    words = fruits + animals + accessories + stationary
    word = random.choice(words)
    print("Your word has", len(word), "letters.")

    print()

    if word in fruits:  # Give category of word
        print("Your word is a Fruit.")
    elif word in accessories:
        print("Your word is related to Accessory.")
    elif word in stationary:
        print("Your word is related to Stationary.")
    elif word in animals:
        print("Your word is an Animal")
    print("Guess the characters")

    guesses = ''

    # any number of turns can be used here
    turns = 5

    while turns > 0:

        # counts the number of times a user fails
        failed = 0

        # all characters from the input word taking one at a time.
        for char in word:

            # comparing that character with the character in guesses
            if char in guesses:
                print(char)

            else:
                print("_")

                # for every failure 1 will be incremented in failure
                failed += 1

        if failed == 0:
            # user will win the game if failure is 0 and 'You Win' will be given as output
            print("You Win")

            # this print the correct word
            print("The word is: ", word)
            break

        # if user has input the wrong alphabet then it will ask user to enter another alphabet
        guess = input("guess a character:")

        # every input character will be stored in guesses
        guesses += guess

        # check input with the character in word
        if guess not in word:

            turns -= 1

            # if the character doesn’t match the word then “Wrong” will be given as output
            print("Wrong")

            # this will print the number of turns left for the user
            print("You have", + turns, 'more guesses')

            if turns == 0:
                print("You Lost! ")
                play_again = input("Would you like to play again? Type Y for yes and N for No!")
                if play_again == "N":
                    repeat = False

And the result:

10.04.png