Last Updated : 11 Jul, 2025
Learn how to create a simple Python word-guessing game, where players attempt to guess a randomly selected word within a limited number of tries.
Word guessing Game in PythonThis program is a simple word-guessing game where the user has to guess the characters in a randomly selected word within a limited number of attempts. The program provides feedback after each guess, helping the user to either complete the word or lose the game based on their guesses.
1. Importing theRandom
Module
import random2. Getting the User's Name, and Greeting the User
The program asks the user for their name using the input() function. This name is stored in the variable name.
name = input("What is your name? ")3. List of Words and Choosing a Random Word
print("Good Luck ! ", name)
A list of possible words for the guessing game is defined. These words are strings stored in a list called words
. Also, the program selects a random word from the words
list using random.choice()
. The selected word is stored in the variable word
.
words = ['rainbow', 'computer', 'science', 'programming',4. Prompting the User to Guess
'python', 'mathematics', 'player', 'condition',
'reverse', 'water', 'board', 'geeks']
word = random.choice(words)
The program prompts the user to start guessing the characters of the randomly chosen word.
print("Guess the characters")5. Initializing Guesses and Turns
guesses
: An empty string that will hold all the characters guessed by the user.turns
: The number of attempts the user has to guess the word. Initially set to 12.guesses = ''6. The Main Game Loop
turns = 12
This while
loop runs as long as the user has remaining turns. Inside the loop, the user will be prompted to guess characters.
while turns > 0:6.1. Checking Each Character in the Word
failed = 0
: A counter for the number of characters that have not been correctly guessed.for
loop iterates through each character in the word.guesses
), it is displayed._
) is displayed, and failed
is incremented.failed = 06.2. Checking if the User Has Won
for char in word:
if char in guesses:
print(char, end=" ")
else:
print("_")
failed += 1
If failed is 0, it means all characters in the word have been guessed correctly. The user wins, and the correct word is displayed. The game ends with a break statement.
if failed == 0:6.3. Prompting for the Next Guess
print("You Win")
print("The word is: ", word)
break
guesses
string.guess = input("guess a character:")6.4. Handling Incorrect Guesses
guesses += guess
if guess not in word:6.5. Checking if the User Has Lost
turns -= 1
print("Wrong")
print("You have", + turns, 'more guesses')
If the user runs out of turns, the game ends with a "You Lose" message.
if turns == 0:7. Ending the Game
print("You Loose")
import random
name = input("What is your name? ")
print("Good Luck ! ", name)
words = ['rainbow', 'computer', 'science', 'programming',
'python', 'mathematics', 'player', 'condition',
'reverse', 'water', 'board', 'geeks']
word = random.choice(words)
print("Guess the characters")
guesses = ''
turns = 12
while turns > 0:
failed = 0
for char in word:
if char in guesses:
print(char, end=" ")
else:
print("_")
failed += 1
if failed == 0:
print("You Win")
print("The word is: ", word)
break
print()
guess = input("guess a character:")
guesses += guess
if guess not in word:
turns -= 1
print("Wrong")
print("You have", + turns, 'more guesses')
if turns == 0:
print("You Loose")
Output
What is your name? Gautam
Good Luck! Gautam
Guess the characters
_
_
_
_
_
guess a character:g
g
_
_
_
_
guess a character:e
g
e
e
_
_
guess a character:k
g
e
e
k
_
guess a character:s
g
e
e
k
s
You Win
The word is: geeks
Word guessing game in Python
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4