0% found this document useful (1 vote)
872 views

Python Program For Word Guessing Game

This program creates a word guessing game in Python. It selects a random word from a list using the random module. It then prompts the user to input their name and guesses for letters in the word. The user has 12 turns to fully guess the word. For each correct guess, the letter is displayed in the word. For incorrect guesses, turns are deducted. If the user guesses the full word before running out of turns, they win. Otherwise they lose.

Uploaded by

Mannam Saran
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
872 views

Python Program For Word Guessing Game

This program creates a word guessing game in Python. It selects a random word from a list using the random module. It then prompts the user to input their name and guesses for letters in the word. The user has 12 turns to fully guess the word. For each correct guess, the letter is displayed in the word. For incorrect guesses, turns are deducted. If the user guesses the full word before running out of turns, they win. Otherwise they lose.

Uploaded by

Mannam Saran
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Python program for word guessing game:

Python is a powerful multi-purpose programming language used by multiple giant


companies. It has simple and easy to use syntax making it perfect language for
someone trying to learn computer programming for first time. It is a high-level
programming language, and its core design philosophy is all about code readability
and a syntax which allows programmers to express concepts in a few lines of code.
In this article, we will use random module to make a word guessing game. This game
is for beginners learning to code in python and to give them a little brief about
using strings, loops and conditional(If, else) statements.

random module :
Sometimes we want the computer to pick a random number in a given range, pick a
random element from a list, pick a random card from a deck, flip a coin, etc. The
random module provides access to functions that support these types of operations.
One such operation is random.choice() method (returns a random item from a list,
tuple, or string.) that we are going to use in order to select one random word from
a list of words that we’ve created.

In this game, there is a list of words present, out of which our interpreter will
choose 1 random word. The user first has to input their names and then, will be
asked to guess any alphabet. If the random word contains that alphabet, it will be
shown as the output(with correct placement) else the program will ask you to guess
another alphabet. User will be given 12 turns(can be changed accordingly) to guess
the complete word.

import random
# library that we use in order to choose
# on random words from a list of words

name = input("What is your name? ")


# Here the user is asked to enter the name first

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 = ''

# any number of turns can be used here


turns = 12

while turns > 0:

# counts the number of times a user fails


failed = 0

# all characters from the input


# word taking one at a time.
for char in word:
print("Guess the characters")

guesses = ''

# any number of turns can be used here


turns = 12

while turns > 0:

# counts the number of times a user fails


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)

else:
print("_")

# for every failure 1 will be


# incremented in failure
failed += 1
if failed == 0:
# user will win the game if failure is 0
# and 'You Win' will be given as output
print("You Win")

# this print the correct word


print("The word is: ", word)
break

# if user has input the wrong alphabet then


# it will ask user to enter another alphabet
guess = input("guess a character:")

# every input character will be stored in guesses


guesses += guess

# check input with the character in word


if guess not in word:

turns -= 1

# if the character doesn’t match the word


# then “Wrong” will be given as output
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")

You might also like