The Word Guessing Game is an exciting and interactive game that requires players to guess a chosen word, letter by letter. Among many things, Python is an excellent language for creating games, so in this tutorial, we will guide you on how to build a Word Guessing Game in Python.
Firstly let us understand the logic and execution of this word guessing game in Python. So there will be a list of words present, out of which our interpreter will choose 1 random word. After that, the user will be asked to input their names and a prompt to “Guess the characters”. The number of characters present in the word selected by the interpreter will be shown by “_”. The player tries to guess this word, letter by letter.
If the guess is correct, the program reveals the letter’s position in the word. The game continues until the player either successfully guesses the word or exceeds the allowed number of incorrect guesses (12 turns in our case), which can be changed consequently.
The objective is to provide a platform to play a game to guess a word present in the sequence of characters using a random module. And if the player guessed the word right, our program prints a “You win” message to the user.
1. You must have a basic understanding of Python. A text editor or an Integrated Development Environment (IDE) where you can write and run your Python code. This can be Notepad++, Sublime Text, PyCharm, or any other text editor of your choice.
import random # on random words from a list of words name = input("What is your name? ") print("Good Luck ! ", name) words = ['rainbow', 'computer', 'science', 'programming', 'python', 'mathematics', 'player', 'condition', 'reverse', 'water', 'board', 'geeks'] # Function will choose one random # word from this list of words word = random.choice(words) print("Guess the characters") guesses = '' turns = 12 while turns > 0: 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, end=" ") else: print("_") # incremented in failure failed += 1 if failed == 0: print("You Win") # this print the correct word print("The word is: ", word) break print() guess = input("guess a character:") guesses += guess if guess not in word: turns -= 1 print("Wrong") # this will print the number of # turns left for the user print("You have", + turns, 'more guesses') if turns == 0: print("You Loose")
Explanation of the code
1. We begin the code by importing the random module, which will be used to select a random word from a list.
2. After that, the user is prompted to enter their name, which is stored in the variable name. A welcome message is printed with their name.
3. A list of words (words) is created. These are the words that the player will have to guess.
4. The random.choice() function is used to select a random word from the words list. This chosen word is stored in the variable word.
5. The user is prompted to start guessing the characters of the word.
6. An empty string guesses is created to keep track of the user’s guesses, and a variable turns is set to 12 to limit the number of wrong guesses a player can make.
7. The script enters a while loop that continues as long as the turns is greater than 0.
- Within this loop, a variable failed is set to 0 at the beginning of each iteration. This variable keeps track of the user’s failed attempts to guess the word.
- The script iterates over each character (char) in the word. If the character is in guesses (i.e., the user has guessed this character correctly), the character is printed. Otherwise, an underscore is printed, and failed is incremented by 1.
- If failed remains 0 after this check (i.e., the user has guessed all characters correctly), a winning message is printed, followed by the correct word, and the loop is exited.
8. After printing an empty line for readability, the script prompts the user to guess a character and appends the guess to guesses.
9. If the guessed character is not in the word, turns is decremented by 1, a “Wrong” message is printed, and the number of remaining guesses is displayed.
10. If turns reaches 0 (indicating that the user has exhausted all their guesses), a “You Loose” message is printed, signaling the end of the game.
Figure 1: Word Guessing wrong characters
Figure 2: Word Guessing Matched
Hence we have successfully developed a Word Guessing Game in Python. By creating this Word Guessing Game, you will not only have a fun game to play but also improve your understanding of Python. You’ll gain hands-on experience with key Python concepts such as string manipulation, control flow, error handling, and more. You can modify the game further as per your requirements.
Sulagna is an experienced Python developer and developed web applications, data analysis tools, and automation scripts using Python. With several years of hands-on experience in Python development, she has gained a deep understanding of the language and its extensive ecosystem of libraries and frameworks. Sulagna has over 5 years of experince as a technical content writer/reviewer. She has contributed articles and tutorials to reputable online platforms, sharing her knowledge and insights on Python development best practices, web development, and data analysis.