Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Hangman Game in Python
Next article icon

Learn how to create a simplePythonword-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 theRandom Module

import random

2.Getting the User's Name, and Greeting the User

The program asks the user for their name using theinput() 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 calledwords. Also, the program selects a random word from thewords list usingrandom.choice(). The selected word is stored in the variableword.

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: 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

6.The Main Game Loop

Thiswhile 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.
  • Thefor loop iterates through each character in the word.
  • If the character has been guessed (i.e., it is inguesses), it is displayed.
  • If the character has not been guessed, an underscore (_) is displayed, andfailed 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

  • The user is prompted to guess a character.
  • The guessed character is added to theguesses string.
guess = input("guess a character:")
guesses += guess

6.4.Handling Incorrect Guesses

  • If the guessed character is not in the word, the number of turns decreases by 1.
  • A "Wrong" message is displayed, along with the number of remaining 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

  • The game loop will end either when the user guesses the word correctly (all characters are guessed) or when the user runs out of turns.

Complete Code

Python
importrandomname=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=12whileturns>0:failed=0forcharinword:ifcharinguesses:print(char,end=" ")else:print("_")failed+=1iffailed==0:print("You Win")print("The word is: ",word)breakprint()guess=input("guess a character:")guesses+=guessifguessnotinword:turns-=1print("Wrong")print("You have",+turns,'more guesses')ifturns==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
Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp