< Back

Lab 5: Text Processing

One of the most common forms of "media" in our daily lives is text. Not only is your textbook made of text, but a majority of social media sites rely fully on text. In addition, you can get books digitally on tablets and e-readers as well. One very common thing that happens with text is transformation. On Facebook, your wall is customized based off what you and your friends have entered into the webpage. Today, we will begin exploring how programs can be written to modify text in this fashion.


Story

In a file called Story.py, write a function called tell_story that prints a story when executed. Your story must be at least 5 lines long, and must include 2 different characters, 2 different actions, and two objects that the two characters are interacting with. The characters, actions, and objects should be specified as a parameter to the tell_story function.

Example

====== Loading Program ======
>>> tell_story("Link", "Gannon", "block", "stab", "master sword", "triforce")
The sky turned a pale blue color as Link reached the top of the staircase.
As Link approached the pedestal holding the master sword, Gannon suddently appeared.
Link lunged for the master sword, but Gannon was able to block the attempt.
Using the power imbuned in him by the triforce, Link summoned the courage to grab the master sword.
Link went to stab Gannon with the master sword, but was unsuccessful in his attempt.
As it turns out, this was only the beginning of their epic encounter.

Hint

The main portion of your story is going to be stored as string literals inside of your program. You need to first decide what your story is going to be, and write those strings inside of your function.

After you have decided what your story will be, abstract out your characters and your objects and your actions. Replace them with variables, which you can define as parameters to the function.

You can add two strings together using the + operator. So, when you replace your elements with variables, you should be able to use the + operator to stick two strings together.


Pyramid

In a file called fancy_pyramid.py, write a function pyramid(char, lines) that prints a series of characters, each on their own line. In this function, char is the character that you want to print in the pyramid, and lines is the number of lines to print. Each line should have one fewer asterisk than the previous line. This is different from the one in the book, which increases the number of characters to each line by a factor of 2. To allow this pyramid to line up, you need to add additonal spaces to the inside of the pyramid. After each character, add a space to the output as well.

Example

====== Loading Program ======
>>> pyramid("*", 10)
 * * * * * * * * * *
  * * * * * * * * *
   * * * * * * * *
    * * * * * * *
     * * * * * *
      * * * * *
       * * * *
        * * *
         * *
          *

Hint

This function will be similar to the program 13 from the book. However, there are several differences that you need to account for.

First, your function can print an arbitrary number of lines, which is specified by the user. This number also specifies the number of characters on the first line of the pyramid.

As you move to the next line, you know you need one fewer character on that line. It also means you need one additional space at the beginning of the line.

Use a for loop so that you can print an arbitrary number of lines. For a given line, if times is your loop variable, you want to print times spaces, and lines - times characters.