Change language

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"  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)



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) 



#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" )


 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 


Learn programming in R: courses


Best Python online courses for 2022


Best laptop for Fortnite


Best laptop for Excel


Best laptop for Solidworks


Best laptop for Roblox


Best computer for crypto mining


Best laptop for Sims 4


Latest questions


Common xlabel/ylabel for matplotlib subplots

12 answers


How to specify multiple return types using type-hints

12 answers


Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers


Flake8: Ignore specific warning for entire file

12 answers


glob exclude pattern

12 answers


How to avoid HTTP error 429 (Too Many Requests) python

12 answers


Python CSV error: line contains NULL byte

12 answers


csv.Error: iterator should return strings, not bytes

12 answers



Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python


How to specify multiple return types using type-hints


Printing words vertically in Python


Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries


Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically