Add A Little Randomness

Posted by Neville on June 18, 2016 under How To, Python, Resources, Training | Comments are off for this article

For Variety

As the little project for building an application that gives users a phrase to guess after the vowels have been removed has developed we have been through these stages.

  1. Build code for removing vowels from a phrase.
  2. Use that code to build a function that can be used repeatedly in the application.
  3. Giving more phrases to use, and then asking the user to guess each one.

Adding Randomness

Up to this point the phrase to be decoded will be the same each time the module is run.

To add variety we need a bigger list of phrases to choose from and a way to randomly select select from that list.

To achieve this  the random module needs to be imported as it is not included by default in the Python code. The random module is one of a library of modules that can be imported for use when needed.

The import random statement is at the top of the code in the figure below.

In this post the application is expanded to:

  • Read phrases from a separate file and make a list of them that can be used for decoding.
  • Randomly select from those phrases to present to the user
  • Screen the phrases so that all phrases for guessing are different.
  • Provide feedback after each guess. The correct phrase if the user did not guess correctly, and a score of how many were correct.

 Reading A File

For this task a text file is read into the code. That text file is a list of movie titles with each title being a line in that file. Read File

A function (loadWords) has been defined to return a list of the movie titles in the text file. In the example shown a constant value for WORDLIST_FILENAME has been given and it is assumed that the text file (movies.txt) is in the same folder as the code for program. If the file is located elsewhere the full address would have to given.

The function loadWords() then does the following:

  1. Prints a note to the console saying that titles are being added.
  2. Opens the file for reading.
  3. Prepares an empty list that will store the movie titles.
  4. Reads each line and appends that line in Title Case (ie a movie title) to the list.
  5. When all titles are added, it prints out the length of the list, which is the number of titles added.
  6. Returns the list of titles for processing.

The Guessing Game.

Guess TitlesAnother function (guessTitle(words)) is defined in the code. This function takes a list of words (in this case, movie titles) and randomly chooses one of the movie titles and passes that title to the previously defined removeVowels function.

It then prints the movie title with the vowels removed and waits for the player to try to type out the name.

If the player guesses correctly 1 point is added to the score. If the guess is incorrect, the right answer is printed out and the player is then able to move on.

The above steps are included in a loop that has a range of 5 steps, so that at the end the player has a score out of 5. The score is initially set to 0, and 1 point is added for each correct guess.

To prevent the same movie title being presented the function has a list of used titles, initially empty, that has the titles used appended. While the random selection produces a used title, that title is ignored and another selection made for the player to guess.

At the completion of the loop the program stops.

2 Commands

After all of the preparation work is done with the variables being given values and the functions defined, it takes just 2 simple commands to set the process rolling.

Those 2 commands come right at the end of code:

  • titles = loadWords()  sets up the list of movie titles to choose from.
  • guessTitle(titles) takes that list, presents titles to guess and prints out a score.


If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Comments are closed.