Unscramble the Word

It has been a while, but here is a short post for a simple game nonetheless.

This game is called “Unscramble the Word”, where the player would be presented a 7-letter word that has been scrambled and would have to provide the unscrambled word as the correct answer to gain a point. A wrong answer would end the game.

This games uses only the very basic of Python functions. For example, choosing a random 7-letter word from a list, changing the sequence of the letters (i.e. string manipulation), and then using an if-else statement to award the point for the correct answer. The game also uses a for loop to allow the player to play up to 5 rounds.

Let us consider the block of code below. The code shown would not start from Line 1 or have continuous Line numbering as with previous post, as some comments/notes present in the code has been omitted for simplicity sake.

Line 10 imports the random module to be used. Line 12 creates a variable that will be used to keep track of the score.
Lines 14 to 17 prints out the instructions of the game in the IPython console when the script is executed.

The game uses a for loop that is repeated 5 times to allow the player 5 rounds of the game, as shown by Line 19.
The word for each round would be chosen from the list as shown in Lines 21 to 24. Here, the list contains 28 words as an example. More words could be added into the list; alternatively, the pandas module could be used to import the word list from a .csv file.

Line 26 chooses a random word from the list described earlier.
To scramble the letters of the chosen word, the word is first assigned as a list itself (Line 27), and the sequence of the letters shuffled (Line 28). The list of scrambled letters is then concatenated into a single string (Line 29) to be displayed in the IPython console (Line 30).

Line 32 assigned the player’s input as a variable, which we named ‘answer‘. The next part of the code (Lines 34 to 43) then uses an if-else statement to award a point if the answer matches the chosen random word and goes on to the next round, or else the game ends with the incorrect answer provided.
In Line 34, the player’s input is first converted to upper case, using the .upper() function, before checking the answer – this allows the player to submit the answer as a lower case or upper case word. See screenshot below for an example of the game.

This presents a basic version of this game, with opportunities to further modify it.

Is there any way to make the above code more concise and elegant? Feel free to comment.

Leave a comment