Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to implement Jumbled word game
A Jumbled Word Game is an engaging word puzzle where players unscramble letters to form meaningful words. This brain-teasing game challenges vocabulary skills and improves problem-solving abilities. In this article, we'll implement a jumbled word game using Python with three different approaches.
Approach 1: Randomizing Letters
The first approach randomly shuffles the letters of a word and displays it to the player. The player must rearrange the letters to form the correct word.
Algorithm
Step 1 ? Define a list of words for the game
Step 2 ? Randomly select a word from the list
Step 3 ? Randomly shuffle the letters of the selected word
Step 4 ? Display the jumbled word to the player
Step 5 ? Read the player's input
Step 6 ? Check if the input matches the original word
Step 7 ? Provide feedback to the player
Step 8 ? Repeat until the player chooses to exit
Example
import random
words = ["papaya", "banana", "orange", "grape", "melon"]
while True:
word = random.choice(words)
jumbled_word = list(word)
random.shuffle(jumbled_word)
jumbled_word = ''.join(jumbled_word)
print("Jumbled word:", jumbled_word)
guess = input("Your guess: ")
if guess.lower() == word.lower():
print("Correct!")
else:
print(f"Incorrect! The correct word was: {word}")
play_again = input("Want to play again? (yes/no): ")
if play_again.lower() != "yes":
break
Jumbled word: egnaro Your guess: orange Correct! Want to play again? (yes/no): no
Approach 2: Creating Anagrams
This approach generates multiple anagrams of a word and displays them as numbered options. The player selects the correct word from the list.
Algorithm
Step 1 ? Define a list of words for the game
Step 2 ? Randomly select a word from the list
Step 3 ? Generate all possible anagrams of the word
Step 4 ? Display the anagrams with numbers
Step 5 ? Read the player's selection
Step 6 ? Check if the selected anagram matches the original word
Step 7 ? Provide feedback to the player
Example
import random
from itertools import permutations
words = ["cat", "dog", "bat", "sun"]
while True:
word = random.choice(words)
# Generate unique anagrams (limit for readability)
anagrams = list(set([''.join(perm) for perm in permutations(word)]))
anagrams = anagrams[:10] # Limit to 10 options
print("Choose the correct word from these anagrams:")
for index, anagram in enumerate(anagrams, start=1):
print(f"{index}. {anagram}")
try:
choice = int(input("Enter the number of your choice: ")) - 1
if 0 <= choice < len(anagrams) and anagrams[choice] == word:
print("Correct!")
else:
print(f"Incorrect! The correct word was: {word}")
except (ValueError, IndexError):
print("Invalid selection!")
play_again = input("Want to play again? (yes/no): ")
if play_again.lower() != "yes":
break
Choose the correct word from these anagrams: 1. cat 2. act 3. tca 4. tac 5. atc 6. cta Enter the number of your choice: 1 Correct! Want to play again? (yes/no): no
Approach 3: Word Scramble with Clues
This approach provides players with both a jumbled word and a helpful clue. Players must unscramble the letters based on the given hint.
Algorithm
Step 1 ? Define a dictionary of words and their clues
Step 2 ? Randomly select a word and its clue
Step 3 ? Display the clue and jumbled letters
Step 4 ? Read the player's input
Step 5 ? Check if the input matches the original word
Step 6 ? Provide feedback to the player
Example
import random
word_clues = {
"apple": "A red fruit that grows on trees",
"banana": "A curved yellow fruit",
"orange": "A citrus fruit with orange skin",
"grape": "Small round fruit that grows in clusters",
"melon": "A large fruit with sweet, juicy 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(f"Clue: {clue}")
print(f"Jumbled word: {jumbled_word}")
guess = input("Your guess: ")
if guess.lower() == word.lower():
print("Correct!")
else:
print(f"Incorrect! The correct word was: {word}")
play_again = input("Want to play again? (yes/no): ")
if play_again.lower() != "yes":
break
Clue: A red fruit that grows on trees Jumbled word: plepa Your guess: apple Correct! Want to play again? (yes/no): no
Comparison of Approaches
| Approach | Difficulty Level | Best For |
|---|---|---|
| Random Letters | Medium | Quick word puzzles |
| Anagrams | Easy | Multiple choice format |
| Clues | Variable | Educational word games |
Conclusion
These three approaches offer different ways to implement a jumbled word game in Python. The random letters approach provides a classic scramble experience, anagrams offer multiple choice options, and clues add an educational element to make the game more engaging and accessible.
