python fairy tale generator

Use Python to create a short text that changes each time you run it. The words in bold below signify things you need to know, to do, or to type.

  1. You’ll need a few files that are located in a folder called fairy_tales
  2. Download this folder to your desktop.  You may need to unzip it once it’s on your desktop.
  3. Double check to make sure it is on your desktop and named fairy_tales
  4. Open terminal (on a Mac it should be in your applications/utilities folder) by double clicking it.
  5. At the prompt, type cd desktop/fairy_tales (this takes you to the folder that you just saved to your desktop)
  6. type python rando.py and press enter.  This will run a simple program that chooses  one element from a list and prints that element.
  7. Now that we know rando.py works, let’s look at it and see how.
  8. Leave Terminal open
  9. Go back to the python folder on the desktop and right click the rando.py file.  Select “open with” and open it with your favorite text editor.  (I like TextWrangler.)
  10. Read this file with your eyeballs. Note that  rando.py is only three lines long.  The first line imports a function called “random”; the second line defines a list called “letters” that is made up of the first five letters of the alphabet.  The third line tells Python to select a random element from the list named “letters.”  When you run this program, that it is what it will do.  That’s it!  Let’s look at another one.
  11. Go back in terminal and type python once.py and press enter.  This will churn out a short fairy tale.  Do this a few times.  See what happens.
  12. (If fairy tales aren’t your thing, try running real.py for something a little more futuristic instead.)
  13. Let’s look at once.py.  Leave Terminal open, but go back to the python folder on the desktop and open once.py file with your favorite text editor.
  14. You’ll see that although this program is longer, the structure is very similar to that of rando.py. Note two important differences: 1) there are now multiple lists with different names and different items that belong to each 2) the output is not a single item from one list but a combination of elements from all of the lists.
  15. Note how the lists are working here.  The fairy tale has a certain feature–say, a magic_object–and within this category there are several different magic objects–mirror, frog, swan, etc–to choose from.
  16. Note that python doesn’t care about any of that.  It cares about the syntax of the list and not the list’s content.
  17. Note that these lists are made of words, which python calls “strings.”
  18. Note the syntax for lists made of strings: name_of_list = [“list item a”, “list item b”, “list item c”]
  19. In particular, note that
    a) the name of the list is one_word (no spaces!)
    b) all the items in the list are surrounded by brackets []
    c) each item is surrounded by quotation marks–single or double, just be consistent
    d) a comma separates each item and goes outside the quotation mark.
  20.  Note the random function (you can still see it at the top of the program) tells Python to pick randomly from these options.  You can have as many categories and as many options within those categories as you like, as long as you follow the syntax exactly. The print function at the end of the program tells Python to pull one option from each category and then print them out in order. Voila!  There you have a very simple fairy tale generator.
  21. Now change the program—make it better, longer, more interesting–by editing the file in your favorite text editor. Here are some options:  1) add a new category/variable and create a list of options for it.   2) add more options to your new variable or to existing variables. 3) change the genre–make it more SF or Mystery or Romance or Adventure or try something else entirely.
  22. You can write over the program or save it with a different_name
  23. Just make sure you save it to the same folder: fairy_tales. If you save it somewhere else, you will have to navigate to that folder.
  24. Make sure it works by testing it in terminal as you go—again, that command is python once.py (if you have changed the file name, then it will be python different_name.py).
Share on FacebookTweet about this on TwitterPin on PinterestShare on Google+

Leave a Reply