MEMORANDUM

 

TO:        Dr. Roberta Albi

 

FROM:  Erika Siebring

 

CC:         ENG 131 Class

 

DATE:    July 14, 2018

 

RE:         How to Code a Mad Lib in Python Programming

 

_____________________________________________________________________________

 

Problem Statement

Problem

Computer programming and creating a sample of computer code are not considered common knowledge. Thus it is unlikely the majority of the ENG 131 class has knowledge of the Python programming software, how to use it or how to create computer code in general.

 

Purpose and Findings

Purpose

This report will detail how to download the Python computer software and write code that creates a single Mad Lib. A Mad Lib is a word game in which an individual produces various words (i.e. noun, adjective, verb) that are substituted for blanks in a narrative template.

 

Findings

The Python software is a user-friendly introduction to computer programming. A Mad Lib is a simple template to explain how the software functions.

 

Recommendation

Individuals in the ENG 131 class should continue to explore the uses of Python as knowledge of it can be applicable to different areas in an individual’s life.






 

 

 

 

 

 

Table of Contents

 

Executive Summary1

Introduction3

Purpose3

            Problem3

            Audience3

Background3

Scope3

Installation of the Python Software4

Python Dashboard4

            Python Shell4

            Creating a New File4

Important Terms & Symbols5

            Addition Sign5

            Comment Sign5

            Expression6

            Input6

            Output6

            Print Statement6

            Program6

Quotation Marks6

String7

Variables7

Coding a Mad Lib8

            “Hello World” Example8

            Introduction to Mad Libs8

            Initiating a Mad Lib Program8

            Creating Mad Lib Variables8

            Implementing Groups of Mad Lib Variables9

            Creating a Story with the Mad Lib Variables10

            Executing a Mad Lib Program11

Common Errors13

            General Error Messages13

            Syntax Errors13

            Runtime Errors14

            Semantic Errors14

Conclusion14

Works Cited15

 

 

 

 

 

 

Introduction

 

Purpose

Problem

Not every individual knows how to create computer code or understands the first steps of computer programming. The ENG 131 class most likely falls into this category and does not have knowledge of Python programming.

 

Audience

The ENG 131 class is the primary audience and main stakeholder. This document is designed to introduce the class to the Python software and how to create computer code. The secondary audience is Dr. Roberta Albi.

 

Background

Started in 1991, Python programming is an object-oriented computer coding language and its software can be downloaded on several different computers. Its simple syntax is widely considered to be one of the easiest platforms to introduce individuals to computer coding. This software is used for general purposes. The official Python website provides various beginner tutorials and its companion website, learnpython.org, is dedicated to helping viewers understand terms and functions within Python. Numerous instructional textbooks have been written about Python as well.

 

Scope

The scope of this project is to explain how to download the Python software and manually code and print one Mad Lib. This includes descriptions of Python’s features and instructions on how to manipulate and print different variables within computer code.















 

 

 

 

 

Installation of the Python Software

 

Free Python downloads are on python.org. SCROLL down the homepage to the ‘Download’ section and click on the latest Python edition (Downloads). A webpage entitled ‘Python 3.7.0’ will appear. The webpage’s name may change as new editions are released. SCROLL down to ‘Files’ where there are versions of Python available for Windows and Mac OS X operating systems (Downloads).

Click on the ‘executable installer’ or ‘installer’ file that corresponds to the computer’s operating system (Downloads). Both the 32-bit and 64-bit systems are available and functional for the Mac OS X system. Click ‘Install Now’ in the pop-up window. The online Python tutorial will be available once the software is installed. Search the computer for ‘Python’ or ‘IDLE’ to access the software. The Python Shell window will open.

 

Python Dashboard

 

Python Shell

The Python Shell window is also called the interpreter (Downey 2). This is where computer code is read and executed. At the top of the Shell window there are various tabs, including ‘File,’ ‘Edit’ and ‘Shell.’ Each of these tabs have various functions listed within them. The >>> line is the prompt (Downey 2). All code in the Shell is written after the prompt.

 

Creating a New File

Computer code can be stored and tested in a file separate from the Shell. Go to the ‘File’ tab in the Shell window and select ‘New File.’ A new file labeled ‘Untitled’ will pop up. Code can be stored in this document, either on a computer or on a flash drive. When saved, the file label will end with ‘.py.’ Go to the ‘Run’ tab in the new file and select ‘Run Module’ to execute any code stored in it. The file will require you to save before closing it. When executed, the code’s result will appear in the Shell window.




 

 

 

 

 

 

 

Important Terms & Symbols

Addition Sign (+)

The + sign works in two ways. It can add two or more whole or decimal numbers (Downey 2). It can also conjoin several pieces of text (Downey 12), or conjoin a punctuation mark with a variable or set of text (See also ‘Variables’ on pg. 7).

Typing an addition problem after the  >>> prompt will result in the sum being printed on a new line. Typing a word followed by + and a comma will result in the word being printed on a new line with a comma next to it (See also ‘Print Statements’ on pg. 6).

 

Figure 1: Symbol Functionality in Python Programming

The symbols in Figure 1 are typically found in mathematics and are used to evaluate numbers in Python. Source: Ekmekci, Berk, et al. "An Introduction to Programming for Bioscientists: A Python-Based Primer." Plos Computational Biology, vol. 12, no. 6, 2016, pp. 1-43. EBSCOhost, doi:10.1371/journal.pcbi.1004867.

 

Comment Sign (#)

The # sign differentiates the functional code from programming information. Any text after this sign is a comment and will be red. Comments in a file often include a file’s name, date, author and description of the code’s functionality. Comments also tell a programmer what each section of code does. Comments can be written after the >>> prompt or in a file.

 

 

 

Expression

An expression is a single line of code (Downey 10). An expression can print text, evaluate numbers or assign a value to a variable.

 

Input

An input is any data (i.e. numbers, equations) the programmer types within the Shell or a file (Downey 1). Anything following the  >>> prompt is an input. Input statements are purple and are written as input(). They can be used to prompt a response from the programmer. For this project, this response will be typing in a word needed for the Mad Lib.

 

Output

An output is the result of executing an expression (See also ‘Expression’ on pg. 6). Outputs are blue on a new line in the Shell (Downey 1), below the  >>> prompt. If an addition problem is typed after the prompt, the sum is the output. To produce an output, enter an input into the Shell and hit ENTER.

 

Print Statement

A print statement is purple and tells the computer to print an expression (Downey 7). Beginning with print(), anything the programmer wants printed goes in the parentheses. Once executed, the output will appear in the Shell on a new line. Print statements with text require either single or double quotation marks within the parentheses (See also ‘Quotation Marks’ on pg. 6). Print statements with integers don’t require quotation marks. To execute printing in the Shell, hit ENTER.

 

To print a number, type out the print statement after the >>> prompt and put the number inside the parentheses. The number will be printed on a new line. Follow this same process when printing a variable (See also ‘Variables’ on pg. 7). To print text, type out the print statement and put the text, in quotation marks, inside the parentheses. The text will be printed on a new line.

 

Program

A program is a sequence of code performing one or more tasks (Downey 6). A program is stored in a Python file on either a computer or flash drive. For this project, the entire Mad Lib will be considered one Python program.

 

Quotation Marks

Quotation marks, single or double, indicate words or sentences. Any text inside quotation marks will be green and will be printed (See also ‘Strings’ on pg. 7). Quotation marks are not needed for numbers or variables (See also ‘Variables’ on pg. 7). Not including quotation marks when printing text will result in an error.

 

Putting double or single quotation marks around the text in a print statement will result in the same output. Typing text with quotation marks immediately after the >>> prompt will produce the same output. Typing text without both the beginning and end quotation marks will result in an error.

 

 

String

A string is an expression with text. It is called this because all the letters or characters are “strung together” (Downey 4). A string must be in quotation marks in order to be printed.

 

Figure 2: Strings in Python Programming

https://lh5.googleusercontent.com/iA9pMr5nc-2ky6I0yAWqDcl2XSTCgoYfdPgr2SHkO5EZZMuYBV1oAHJdIp6bYrkF4LVTqAJJOP6SG683rPbSuR1oAxNwuzPMF95U7qJRzys_PqKcCBhvumNs0HBGQbjz1l_szu6i

 

The variable in Figure 2 is named ‘mystring,’ which has the assigned value of ‘hello.’ When the variable is printed, the result will be ‘hello.’ Source: "Variables and Types." Learn Python, n.d., www.learnpython.org. Accessed 16 June 2018.

 

Variables

A variable is a name that refers to a value (Downey 9). This value can be a number or string. Variables cannot start with a number or be a Python keyword (Downey 10). Keywords are orange and perform functions not described in this document. Attempting to use a keyword as a variable will result in an error. A variable does not need quotation marks.

 

A variable can be given a number value by typing the name of the variable, an equal sign, and a number. A print statement with the variable name in the parentheses will produce the number as an output. A variable can have a string value by following the same process and putting the string in quotation marks. The print statement will return the string as an output.

 

Python’s keywords include: False, None, True, and, as, assert, break, class, continue, def, del, elif, else, except, finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or, pass, raise, return, try, while, with, yield (Downey 10).

 

 

Coding a Mad Lib

 

“Hello, World!” Example

The most basic computer exercise is the “Hello, World!” program (Perkel 125). It’s the first expression typed when working with a new coding language. Programming a Mad Lib is similar to the “Hello, World!” print statement.

 

A print statement with the text “Hello, World!” in the parentheses can either use single or double quotation marks. Both result in the Hello, World! output.

 

Introduction to Mad Libs

A Mad Lib is a word game where an individual produces various words (i.e. noun, adjective, verb) that are substituted for blanks in a narrative template. This project will create an introductory message explaining the directions and several expressions asking for user input. The result is an entire printed Mad Lib.

 

Initiating a Mad Lib Program

Open the Python software. Go to the ‘File’ tab and select ‘New File.’ A new, untitled file will appear. Title this file ‘Movies Should Be Fun Mad Lib.’

 

In the ‘Movies Should Be Fun’ file, create a large rectangle using the # symbol. Everything typed inside the box will now be a comment. Put the title of the file inside the box at the center top. Move the cursor down a few lines. Type Programmed by followed by your name and the date and align it to the left. Move the cursor down two lines. Type Description: followed by a few sentences explaining how this project will create one Mad Lib. For easy reading, begin typing the description and stop when the words stop fitting inside the box. Move the cursor underneath the first line and type the remainder of the description. Delete any unnecessary spaces until the # symbols form straight horizontal and vertical lines.

 

Move the cursor outside the bottom right corner of the box. Hit ENTER and create a #Intro comment. Hit ENTER again and create an input statement. In the parentheses, create a string that welcomes the user to the Mad Lib. When the program is executed, this expression will greet the user.

 

Creating Mad Lib Variables

Hit ENTER twice and create a #User Input comment. This indicates the start of the Mad Lib. Hit ENTER and type nounBuildings to create the first variable. Type an equal sign and an input statement. In the parentheses, create a string that asks the user to enter a plural noun. Finish the string with a space, and an arrow made of an equal sign and a greater-than sign. When the program is executed, this expression will ask the user to type in a plural noun for the Mad Lib. That plural noun will be the variable nounBuildings’s value.

 

 

 

 

Implementing Groups of Mad Lib Variables

Now one variable with one Mad Lib component has been created. More variables will be created and assigned values using the above structure. Hit ENTER and type adjectiveExtinct to create the second variable. Type an equal sign and an input statement. In the parentheses, create a string asking the user to input an adjective. Finish the string with a space, and an arrow made of an equal sign and a greater-than sign. When the program is executed, this expression will ask the user to type in an adjective for the Mad Lib. That adjective will be the variable adjectiveExtinct’s value.

 

Hit ENTER and create the variable nounMonsters followed by an equal sign and an input statement. In the parentheses, create a string asking the user to input a plural noun and finish it with a space and an arrow. Like the other expressions, this will ask the reader for a different part of the Mad Lib. This pattern will continue through all the following expressions.

 

Hit ENTER and create the variable nounBed followed by an equal sign and an input statement. In the parentheses, create a string asking the user to input a noun. Finish the string with a space and an arrow.

 

Hit ENTER and create the variable adjectiveWholesome followed by an equal sign and an input statement. In the parentheses, create a string asking the user to input an adjective. Finish the string with a space and an arrow.

 

Hit ENTER and create the variable nounLife followed by an equal sign and an input statement. In the parentheses, create a string asking the user to input a noun. Finish the string with a space and an arrow.

 

Hit ENTER and create the variable nounWind followed by an equal sign and an input statement. In the parentheses, create a string asking the user to input a noun. Finish the string with a space and an arrow.

 

Hit ENTER and create the variable personintheroomSmith followed by an equal sign and an input statement. In the parentheses, create a string asking the user to input the name of a person. Finish the string with a space and an arrow.

 

Hit ENTER and create the variable placeWashington followed by an equal sign and an input statement. In the parentheses, create a string asking the user to input the name of a place. Finish the string with a space and an arrow.

 

Hit ENTER and create the variable adjectiveWarm followed by an equal sign and an input statement. In the parentheses, create a string asking the user to input an adjective. Finish the string with a space and an arrow.

 

Hit ENTER and create the variable personintheroomHardy followed by an equal sign and an input statement. In the parentheses, create a string asking the user to input the name of a person. Finish the string with a space and an arrow.

 

Hit ENTER and create the variable personintheroomCostello followed by an equal sign and an input statement. In the parentheses, create a string asking the user to input the name of a person. Finish the string with a space and an arrow.

 

Hit ENTER and create the variable adjectiveFunny followed by an equal sign and an input statement. In the parentheses, create a string asking the user to input an adjective. Finish the string with a space and an arrow.

 

Hit ENTER and create the variable nounComedyRoutines followed by an equal sign and an input statement. In the parentheses, create a string asking the user to input a plural noun. Finish the string with a space and an arrow.

 

Hit ENTER and create the variable partofthebodySides followed by an equal sign and an input statement. In the parentheses, create a string asking the user to input a plural part of the body. Finish the string with a space and an arrow.

 

Creating a Story with the Mad Lib Variables

Each variable will be incorporated into a narrative. Hit ENTER and create a #Print Story comment. Hit ENTER again and create a print statement under the comment. In the parentheses, type the string: "In recent years, there have been too many disaster movies in which tall". This will be printed when the code is executed. After the end of the string, type a comma, nounBuildings and another comma. When the program is executed, the variable nounBuildings will be printed immediately after the end of the string.

 

Hit ENTER and create a new print statement. In the parentheses, type the string "catch on fire," followed by a comma, adjectiveExtinct, and a comma. Type the string "dinosaurs come to life, and huge", then a comma, nounMonsters, a comma, and the string "attack people in the". When executed, this will print the variables and strings as part of the narrative.

 

Hit ENTER and create a new print statement. In the parentheses, type the string "ocean, making you afraid to get out of your”, then a comma, nounBed, a comma and the string "in the morning.".

 

Hit ENTER and create a new print statement. In the parentheses, type the string "Movie fans ask why we can't have more”, then a comma, adjectiveWholesome, a comma, the string "pictures like It's a Wonderful” another comma, and then nounLife with a + and the string “,”.

 

Hit ENTER and create a new print statement. In the parentheses, type the string "Gone with the”, then a comma, nounWind, a +, and the string ", or Mr.". Type a comma, personintheroomSmith, a comma, the string "Goes to", a comma and placeWashington with a + and the string “.”.

 

Hit ENTER and create a new print statement. In the parentheses, type the string "These films made you feel", then a comma, adjectiveWarm, a comma and the string "all over. These same fans also ask why we can't".

 

Hit ENTER and create a new print statement. In the parentheses, type the string "have more funny films with comedians such as Laurel and" and then a comma. Type personintheroomHardy, a +, the string ", and Abbott and", a comma, personintheroomCostello, a + and the string “.”.

 

Hit ENTER and create a new print statement. In the parentheses, type the string "These" and a comma, adjectiveFunny, a comma and the string "performers gave us great slapstick". Type a comma, nounComedyRoutines, a comma, the string "that still makes our", a comma, partofthebodySides and a comma.

 

Hit ENTER and create a new print statement. In the parentheses, type the string "ache from laughing.".

 

Hit ENTER and create the nounTheEnd variable with an equal sign and an input statement. In the parentheses, type the string  The End”.

 

When the program is executed, each print statement will print a portion of the Mad Lib. When variable names are followed by + and a comma, the + adds a comma into the printed text.

 

Executing a Mad Lib Program

To execute the program, go to the ‘Run’ tab at the top of the file and select ‘Run Module.’ Hitting F5 will also execute the program. The file will automatically save before executing. The Python Shell window will open and print the welcome message in blue.

 

Hit ENTER to continue. The next line will ask you to input a plural noun. Type in a plural noun of your choice. Hit ENTER. The next line will ask you to input an adjective. Type in an adjective of your choice. Hit ENTER. Continue this process until the Mad Lib program prints the full narrative. The full output will look like this:

 

In recent years, there have been too many disaster movies in which tall Input catch on fire, Input dinosaurs come to life, and huge Input attack people in the ocean, making you afraid to get out of your Input in the morning.

Movie fans ask why we can't have more Input pictures like It's a Wonderful Input, Gone with the Input, or Mr. Input Goes to Input.

These films made you feel Input all over. These same fans also ask why we can't have more funny films with comedians such as Laurel and Input, and Abbott and Input.

These Input performers gave us great slapstick Input that still makes our Input ache from laughing.

The End.

In this output, ‘Input’ replaces the user-inputted values. Hit ENTER to end the program. The >>> prompt to appear. Hit F5 or select ‘Run Module’ in the file window to execute the program again. The narrative will change slightly if the user inputs different values.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Common Errors

 

General Error Messages

A programmer can make mistakes when creating code. A mistake in the ‘Movies Should Be Fun’ file will cause the code to stop executing. If so, a separate window will open indicating the error type and the affected line of code.

If a mistake occurs in the Python Shell, a red error message will print.

 

Syntax Errors

Syntax errors are the most common type of error. These occur when the code structure is incorrect or incomplete (Downey 13). Common syntax errors are:

 

I.                    Forgetting the Quotation Marks

There are two ways an error like this can look.

If after the >>> prompt a string is typed without an ending quotation mark, the error SyntaxError will appear.

If a print statement includes text without quotation marks, the error Traceback: NameError will appear.

 

To fix the first error, complete the string with an end quotation mark. To fix the second error, include quotation marks around the text inside the print statement’s parentheses. Review ‘Quotation Marks’ in the Important Terms section for more information.

 

  1. Forgetting a Parenthesis

This error occurs when including only one parenthesis in a print statement.

 

In the Shell, the cursor will stay under the incomplete print statement until it’s completed with another parenthesis. In a program file, the same error will read: Unexpected EOF while parsing. Add another parenthesis.

 

  1. Forgetting a Comma

This error occurs when forgetting a comma when combining a string and a variable in a print statement. This will result in a SyntaxError.

 

To fix this error, add a comma in between the end of the string and the variable.

 

  1. Typing an Invalid Variable Name

This error occurs when attempting to create a variable with a Python keyword. This will result in a SyntaxError error. This error also occurs when a previously-used variable name is misspelled.

                             

To fix the first error, pick a new variable. Review ‘Variables’ in the Important Terms section for more information. To fix the second error, review the program to make sure all variables are spelled correctly.

 

 

Runtime Errors

Runtime errors occur while the program executes (Downey 14). If an error is found, the program will stop and return an error message. A red bar will appear where the mistake is. Review the program carefully to fix the error.

 

Semantic Errors

Semantic errors occur when a program’s result does not say what the programmer wants (Downey 14). Semantic errors include misspelling a string or forgetting punctuation in a string. Closer evaluation is needed.

 

Conclusion

 

This report explored the functionality of Python programming and the common errors that prevent the functionality of a program. Educating the 131 class about Python will result in the spreading of computer programming knowledge and knowledge of how computers work. This could in turn be useful for personal fulfillment or professional work. It is recommended that those whom are interested in computer programming continue to expand their knowledge on the subject.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Works Cited

 

Downey, Allen B. Think Python: How to Think like a Computer Scientist, ed. 2, no. 2.2.23, 2015, pp. 1-244. O’Reilly Media, Inc., greenteapress.com/thinkpython2/thinkpython2.pdf. Accessed 16 June 2018.

“Downloads.” Python, 2001, www.python.org. Accessed 16 June 2018.

Perkel, Jeffrey M. "Programming: Pick up Python." Nature, vol. 518, no. 7537, 2015, pp. 125-126. EBSCOhost, doi:10.1038/518125a.