Python program for guessing words



In this article, we will use a random module to make a word guessing game. This game is for beginners who learn to program in python and give them a little information 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.

This game has a list of present words, from which our translator will select 1 random word. The user must first enter their names, and then, they will be asked to guess any alphabet. If a random word contains this alphabet, it will be shown as output (with correct placement), otherwise the program will ask you to guess a different alphabet. The user is given 12 moves (can be changed accordingly) to guess the full word.

Below is the Python implementation:

import random

# the library we use to select
# for random words from the wordlist

  

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

# Here the user is asked to enter a name first

 

print ( "Good Luck!" , name) < / code>

 

words = [ `rainbow` , `computer` , ` science` , `programming`

  `python` , ` mathematics` , ` player` , `condition`

`reverse` , `water` , ` board` , `gee ks`

 
# The function will pick one random
# word from this wordlist

word = random.choice (words)

 

 

print ( "Guess the characters" )

 

guesses = ` `

 
# any number of moves can be used here

turns = 12

  

  

while turns & gt;  0 :

 

# counts the number of unsuccessful user attempts

  failed = 0

 

# all characters from input

# word accepting one at a time.

for char in word: 

 

  # comparing this character to

 < / code> # guessing character

if char in guesses: 

print (char)

 

else

print ( "_" )

 

# for each failure 1 there will be

  # increased on failure

  failed + = 1

  

  

  if failed = = 0 :

# the user wins the game if the error is 0

# and “You won "Will be given as output

  print ( "You Win"

 

# type the correct word

  print ( " The word is: " , word) 

break

 

#if the user entered an incorrect alphabet, then

# he will ask the user to enter a different alphabet

guess = input ( " guess a character: " )

 

# every input character will be conjectured

  guesses + = guess 

 

# check input with a character in a word

if guess not in word:

 

turns - = 1

 

# if the character does not match the word

# then "Invalid" will be given as output < / code>

print ( "Wrong" )

 

# this will print the number

# rotate left for user

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: gg _ _ _ _ guess a character: egee _ _ guess a character: kgeek _ guess a character: sgeeks You Win The word is: geeks