Build Python Project : Word Scramble Game

Create a word scramble game using lists, tuples, and sets.

Ahmad Mizan Nur Haq
Data And Beyond
4 min readNov 30, 2023

--

The Word Scramble Game is an interactive and educational tool that challenges players to decipher scrambled words. The game utilizes various data structures to enhance its functionality and provide an engaging experience.

Lets planning the project

First we need to know what we want to build, and here it is ;

  1. The game should present the user with a scrambled word and ask them to guess the correct word.
  2. The game should keep track of the user’s score and provide feedback after each guess.

Here is what will learn while building this project;

  1. Lists: Lists, being mutable collections of items, serve as the primary data storage for the game. They hold the list of words to be scrambled and store the user’s guesses.
  2. Tuples: Tuples, immutable sequences of data, are employed to store the correct versions of scrambled words. The immutability of tuples ensures the integrity of the correct words, preventing unintentional modifications.
  3. Sets: Sets, collections of unique elements, prove useful for checking user guesses against the correct word. Sets offer efficient membership checks, enabling quick validation of the user’s input.

Data Collection Manipulation.

How the gameplay mechanics ;

  1. The game begins by randomly selecting a word from the list and scrambling it using a dedicated function.
  2. The scrambled word is presented to the player, challenging them to decipher the original word.
  3. The player is prompted to enter their guess for the unscrambled word.
  4. The user’s guess is compared to the correct word using sets. If the guess matches, the player is informed of their success.
  5. A score counter tracks the player’s correct guesses, providing a measure of their performance.
  6. The game continues until the player successfully unscrambled the word, at which point their final score is displayed.

Let’s Build The Project

  1. Import Libraries
import random

2. Define Word List

words = ["python", "programming", "algorithm", "data", "structure", "analysis", "science", "statistic"]

Create a list of words to be used in the game.

3. Define Word Scrambler Function

def word_scrambled(word):
word_scrambled = ''.join(random.sample(word, len(word)))
return word_scrambled

This function takes a word as input and returns a scrambled version of that word. It uses the random.sample() function to randomly select characters from the word and join them together into a new string.

4. Initialize Variables

score = 0
attempts = 0

These lines initialize two variables: score and attempts. The score variable will keep track of the number of correct guesses the user has made, and the attempts variable will keep track of the number of attempts the user has made in the current round.

5. Main Game

while True:
# Select a word and scramble it
selected_word = random.choice(words)

# Set the maximum number of attempts
max_attempts = 3

# Start the guessing loop
while attempts < max_attempts:
guess = input("Guess the scrambled word: " + word_scrambled(selected_word)) # Remove the second call to word_scrambled

# Check if the guess is correct
if guess == selected_word:
score += 1
attempts += 1
print("Correct! You guessed the word in " + str(attempts) + " attempts.")
break

else:
attempts += 1
print("Incorrect. Try again.")

# Handle the case of reaching the maximum number of attempts
if attempts == max_attempts:
print("Game Over! You ran out of attempts. The correct word was: " + selected_word)
break

# Allow the user to continue or quit
play_again = input("Play again? (Y/N): ")
if play_again.lower() != "y":
print("Thank you for playing! Your final score is: " + str(score))
break

This loop runs the game repeatedly until the player decides to quit. Inside the loop, a new word is selected, scrambled, and displayed to the player. The player is prompted to guess the unscrambled word. If the guess is correct, the score is incremented, and the loop continues to the next word. If the guess is incorrect, the player is given another attempt. If the player reaches the maximum number of attempts without guessing the word, the game ends, and the correct word is revealed. After each round, the player is asked if they want to continue playing. If they choose to quit, their final score is displayed.

Let’s Test it

Conclusion

The Word Scramble Game demonstrates the versatility and applicability of data structures in Python programming. It showcases how lists, tuples, and sets can be combined to create engaging and educational tools.

Hey 👋 Enjoying the content? If you find it valuable, why not subscribe and follow Me on Medium for more content?

🔔 Subscribe & Follow

☕️Buymeacoffee |📚Substack | GitHub | LinkedIn

By subscribing, you’ll get the latest updates, tips, and content delivered right to your inbox.

Thank you for your support! 🙌

In this post, I kindly want to see the effectiveness of Call to Action (CTA).

--

--