Word Guessing Game

                                      Word Guessing Game

       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.

                           Below is the Python implementation: 

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)
else:
print("_")
failed += 1
if failed == 0:
print("You Win")
print("The word is: ", word)
break
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:

malWhat is your name? kittu
Good Luck !  kittu
Guess the characters
_
_
_
_
_
guess a character:w
w
_
_
_
_
guess a character:a
w
a
_
_
_
guess a character:t
w
a
t
_
_
guess a character:o
Wrong
You have 11 more guesses
w
a
t
_
_
guess a character:e
w
a
t
e
_
guess a character:r
w
a
t
e
r
You Win
The word is:  water
>>> 

1 comment:

  1. For example, suppose the sample consists of wins and losses in a sequence of bets. Then we'd simply have an interest in the total amount of cash received, somewhat than the entire sequence of wins and losses. Besides enabling improved buyer relations, central servers save casinos big cash} in different methods. For occasion, server methods help casinos comply with native gaming 카지노 사이트 regulations.

    ReplyDelete

Powered by Blogger.