Python 005: Text Generation
This will help you:
Work with dictionaries, read and write text files, and generate semi-random text.
You can create some truly amusing things with a random text generator. In this activity, you'll learn to write your own version of a Markov chain generator, and soon you'll be churning out the next New York Times Worstseller - leaving all the creativity to the computer. If you're pretty confident, skip to the main activity, or do the warm-ups first.
Time: 1-2 hours / Level: B2
You should already:
Know the basics of Python syntax (see Python Basics 1)
Get the code and resources for this activity by clicking below. It will allow you to download the files from a Google Drive folder. Unzip the folder and save it in a sensible location.
Step 1: Warm-up - Working with files
file_ops.py and look through it. Once you have looked it over, run it by typing
python file_ops.py in the terminal. Complete the following tasks and read the code and comments as you go.
file cursor: Fix the code so the file will be read again, print backwards, and then print each of the first 10 words on a line.
read-only files: Keep the code from giving an error by commenting out the line that doesn't work.
write-only files: Keep the code from giving an error by commenting out the 2 lines that don't work.
w_fileis closed, open it to view its contents, then watch what happens when you reopen it with
rw_fileis closed, view its contents, then watch what happens when you reopen and edit it with