Python Program to implement Jumbled word game


Are you prepared to put your word aptitudes to the test? The Jumbled Word Game is here to challenge your ability to unscramble letters and form meaningful words. Get ready to jumble and unravel words as you lock in this engaging and brain-teasing amusement. Jumbled word games are a fun way to challenge your vocabulary aptitudes and improve your problem-solving capacities.

In this article, we'll learn how to execute a jumbled word game utilizing Python programming language. We'll investigate three diverse approaches to unravel the diversion, each with its possess calculation and code execution.

Approach 1: Randomizing Letters

The primary approach includes arbitrarily rearranging the letters of a given word and displaying it to the player. The player at that point must modify the letters to make the right word. Let's jump into the step-by-step handle of actualizing this approach.

Algorithm

  • Step 1 − Begin by characterizing a list of words to be utilized within the game.

  • Step 2 − Arbitrarily select a word from the list.

  • Step 3 − Arbitrarily rearrange the letters of the chosen word.

  • Step 4 − Display the rearranged word to the player.

  • Step 5 − Studied the player's input.

  • Step 6 − Check on the off chance that the input matches the primary word.

  • Step 7 − Deliver input to the player concerning their answer.

  • Step 8 − Rehash the strategy until the player must exit the entertainment.

Example

import random
words = ["Papaya ", "banana", "orange", "grape", " water melon"]

while True:
    
   word = random.choice(words)

   jumbled_word = list(word)
   random.shuffle(jumbled_word)
    
   jumbled_word = ''.join(jumbled_word)
    
   print("Jumbled input :", jumbled_word)
    
   guess = input("Player guess: ")
    
   if guess == word:
      print("Correct!")
   else:
      print("Incorrect!")

   play_again = input("Want to play again? (yes/no): ")
   if play_again.lower() != "yes":
      break

Output

umble input : noager
Player guess: orange
Correct!
Do you want to play again? (yes/no): yes
Jumbled Input: pnalep
Player guess: apple
Correct!
Do you want to play again? (yes/no): no

Approach 2: Creating Anagrams

The second approach includes creating all conceivable anagrams of a given word and displaying them to the player. The player at that point ought to select the proper word from the list of re-arranged words. Let's investigate the calculation and usage of this approach.

Algorithm

  • Step 1 − Begin by characterizing a list of words to be utilized within the game.

  • Step 2 − Arbitrarily select a word from the list.

  • Step 3 − Produce all possible anagrams of the chosen word.

  • Step 4 − Show the anagrams to the player.

  • Step 5 − Perused the player's input.

  • Step 6 − Check if the input matches the first word.

  • Step 7 − Give input to the player regarding their reply.

  • Step 8 − Rehash the method until the player needs to exit the game.

Example

import random
from itertools import permutations

words = ["apple", "banana", "orange", "grape", "melon"]

while True:
    
   word = random.choice(words)

   anagrams = [''.join(perm) for perm in permutations(word)]

   print("Anagrams:")
   for index, anagram in enumerate(anagrams, start=1):
      print(index, ".", anagram)
    
   guess = int(input("number corresponding to your guess: ")) - 1
    
   if anagrams[guess] == word:
      print("Correct!")
   else:
      print("Incorrect!")

   play_again = input("Do you want to play again? (yes/no): ")

   if play_again.lower() != "yes":
      break

Output

Anagrams:
1 . grape
2 . graep
3 . grpae
4 . grpea
5 . greap
6 . grepa
7 . garpe
8 . garep
9 . gapre
10 . gaper
11 . gaerp
12 . gaepr
13 . gprae
14 . gprea
15 . gpare
16 . gpaer
17 . gpera
18 . gpear
19 . gerap

Approach 3: Word Scramble Clues

The third approach includes showing the player a clue comprising of the jumbled letters and a definition of the word. The player should unscramble the letters to make the right word based on the given clue. Let's investigate the calculation and usage of this approach.

Algorithm

  • Step 1 − Begin by characterizing a list of words and their comparing clues.

  • Step 2 − And then select a word and its clue from the list.

  • Step 3 − Show the clue and cluttered letters to the player.

  • Step 4 − Considered the player's input.

  • Step 5 − Check in case the input matches the beginning word.

  • Step 6 − Grant input to the player concerning their answer.

  • Step 7 − Go over the strategy until the player must exit the game.

Example

import random

word_clues = {
   "apple": " red grows on trees",
   "banana": " curved fruit with a yellow skin",
   "orange": " orange skin",
   "grape": " round fruit that grows in clusters.",
   "melon": " fruit with juicy, sweet flesh."
}

while True:
    
   word, clue = random.choice(list(word_clues.items()))
 
   jumbled_word = list(word)
   random.shuffle(jumbled_word)
 
   jumbled_word = ''.join(jumbled_word)

   print("Clue:", clue)
   print("Jumbled Input:", jumbled_word)

   guess = input("Player  guess: ")

   if guess == word:
      print("Correct!")
   else:
      print("Incorrect!")
    
   play_again = input("Do you want to play again? (yes/no): ")

   if play_again.lower() != "yes":
      break

Output

Clue: red and grows on trees.
Jumbled Input: plape
Player guess: apple
Correct!
Do you want to play again? (yes/no): yes
Clue: orange skin
Jumbled Input : oernag
Player guess: orange
Correct!
Do you want to play again? (yes/no): no 

Conclusion

In this article, we have executed a jumbled word game utilizing three diverse approaches in Python. Each approach gives a one-of-a-kind way to challenge the player's vocabulary abilities and problem-solving capacities. By randomizing letters, creating anagrams, or giving word scramble clues, players can have an engaging and entertaining experience whereas improving their information.

Updated on: 29-Aug-2023

356 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements