In this article, we will use a random module to make a word guessing game. This game is for beginners who learn to program in python and give them a little information about using strings, loops and conditional (If, else) statements.
random module :
Sometimes we want the computer to pick a random number in a given range, pick a random element from a list, pick a random card from a deck, flip a coin, etc. The random module provides access to functions that support these types of operations. One such operation is
random.choice ()method (returns a random item from a list, tuple, or string.) That we are going to use in order to select one random word from a list of words that we`ve created.
This game has a list of present words, from which our translator will select 1 random word. The user must first enter their names, and then, they will be asked to guess any alphabet. If a random word contains this alphabet, it will be shown as output (with correct placement), otherwise the program will ask you to guess a different alphabet. The user is given 12 moves (can be changed accordingly) to guess the full word.
Below is the Python implementation:
What is your name? Gautam Good Luck! Gautam Guess the characters _ _ _ _ _ guess a character: gg _ _ _ _ guess a character: egee _ _ guess a character: kgeek _ guess a character: sgeeks You Win The word is: geeks