![]() |
VOOZH | about |
A word guessing game is a project that demonstrates the use of random selection, loops, conditional statements and string manipulation. In this game, the program randomly selects a word and the player must guess its characters within a limited number of attempts.
The game begins by randomly choosing a word from a predefined list and hiding its characters. The player then guesses letters one at a time, with correct guesses revealing matching characters and incorrect guesses reducing the available attempts. The game ends when the word is fully revealed or all chances are exhausted.
Random Moduleimport random
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)
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)
The program prompts the user to start guessing the characters of the randomly chosen word.
print("Guess the characters")
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 = ''
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 = 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
guesses string.guess = input("Guess a character: ")
if len(guess) != 1:
print("Please enter a single character.")
continue
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 Lose")
Output
What is your name? Kevin
Good Luck! KevinGuess the characters
_ _ _ _ _ _ _
Guess a character: a
Wrong
You have 11 more guesses
_ _ _ _ _ _ _
Guess a character: gg _ _ _ _
Guess a character: eg e e _ _
Guess a character: kg e e k _
Guess a character: sg e e k s
You Win
The word is: geeks