A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-program-for-word-guessing-game/ below:

Python Program for word Guessing Game

Python Program for word Guessing Game

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 Python

This 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 the Random Module
import random
2. 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? ")
print("Good Luck ! ", name)
3. List of Words and Choosing a Random Word

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',
'python', 'mathematics', 'player', 'condition',
'reverse', 'water', 'board', 'geeks']
word = random.choice(words)
4. Prompting the User to Guess

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 = ''
turns = 12
6. The Main Game Loop

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
for char in word:
if char in guesses:
print(char, end=" ")
else:
print("_")
failed += 1
6.2. Checking if the User Has Won

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:
print("You Win")
print("The word is: ", word)
break
6.3. Prompting for the Next Guess
guess = input("guess a character:")
guesses += guess
6.4. Handling Incorrect Guesses
if guess not in word:
turns -= 1
print("Wrong")
print("You have", + turns, 'more guesses')
6.5. Checking if the User Has Lost

If the user runs out of turns, the game ends with a "You Lose" message.

if turns == 0:
print("You Loose")
7. Ending the Game Complete Code Python
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