The Python programming language lets you generate humorous memos automatically. This document includes an example Python 3 program that generates random humorous memos. This code includes word lists (arrays), function definitions, and nested while loops. The word lists contain various verbs, adverbs, objects, adjectives and subjects for use in sentences. The program code defines functions that select random words to generate sentences. The nested while loops generate paragraphs and sentences.
Python is free to download, install, and use. It's easy to program and delightful to run. Before trying the example program in this document, you need to install Python 3 on your computer. For more information, see Getting started with Python programming. For information about random functions, see Using Python random functions to create new words.
When the code in Figure 3 calls the title () function, it makes three random.choice () function calls that select:
Python is free to download, install, and use. It's easy to program and delightful to run. Before trying the example program in this document, you need to install Python 3 on your computer. For more information, see Getting started with Python programming. For information about random functions, see Using Python random functions to create new words.
Example Python program for automatic memos
Figures 1 through 3 show example Python code that can generate automatic memos. To run this code on your computer, do the following:- Install Python 3 if you've not already done so.
- Run the interface development environment (IDE) for your Python 3.
- Copy AutomaticWriter and then paste it into an appropriate Python 3 window.
- Run the program and, at the prompt, type the number of paragraphs you want in your memo, such as 4.
- Press Enter to generate and display a random memo with a random title, such as 'Radically Downsize Plaintiffs."
- Repeat steps 4 and 5 until the program generates a sufficiently humorous memo. You can save your memo by copying its text to a word processor.
- Experiment with this program by modifying your copy of it. For example, you can add words and phrases most appropriate to your friends.
Word lists
The Python lists in Figure 1 contain the possible words and phrases that can appear in a memo generated by this Python program. The name of each list describes the grammatical function of its items:
- ucverb -- upper-case verbs, such as Exploit or Ridicule
- lcverb -- lower-case verbs, such as exploit or ridicule
- ucadvrb -- upper-case adverbs, such as Carefully or Instantly
- lcadvrb -- lower-case adverbs, such as carefully or instantly
- objct -- objects, such a customers or safety inspectors
- adjctv -- adjectives, such as uninspired or repugnant
- sbjct -- subjects, such as We or Human resources
- auxlry -- auxiliaries, such as should always or must
- ttlobj -- title objects, such as Regulators or Wall Street Brokers
Function definitions
The functions defined in Figures 1 and 2 make random.choice () function calls to return random words or phrases for the title and six sentence types. For example, the title () function is:
def title (ucadverb,ucverb,ttlobj):
a = random.choice (ucadvrb)
b = random.choice (ucverb)
b = random.choice (ucverb)
c = random.choice (ttlobj)
r = a + ' ' + b + ' ' + c
return (r)
r = a + ' ' + b + ' ' + c
return (r)
When the code in Figure 3 calls the title () function, it makes three random.choice () function calls that select:
- One upper-case adverb from the ucadvrb list
- One verb from the ucverb list
- One title object from the ttlobj list
- The sntnc01 () function makes three random.choice () function calls that select three words for sentence type 01, concatenates those words, and then returns them as a sentence type 01 within the automatic memo. Note: Figure 1 includes the sntnc01 () function. Figure 2 includes the sntnc02 () through sntnc06 () functions.
- The sntnc02 () function makes three random.choice () function calls that select three words for sentence type 02, concatenates those words, and then returns them as a sentence type 02 in the automatic memo.
- The sntnc03 () function selects, concatenates and returns four words for sentence type 03.
- The sntnc04 () function selects, concatenates and returns five words for sentence type 04.
- The sntnc05 () function selects, concatenates and returns four words for sentence type 05.
- The sntnc06 () function selects, concatenates and returns five words for sentence type 06.
Nested while loops
The Python code in Figure 3 generates a random memo as follows:
- The title () function generates a random title.
- The outer while loop generates a user-specified number of paragraphs. Each pass through the outer (paragraph) loop:
- Calls the random.randint () function to specify between three to nine sentences as the paragraph length.
- The inner loop generates sentences. Each pass through the inner (sentence) loop:
- Calls the the range (1,6) function to set the switch value to 1 to 6.
- Calls the random.choice (switch) function to call a sentence-type function, sntnc01 () to sntnc06 (). The sentence-type function generates its random sentence.
- Concatenates the sentence to the paragraph.
- Calls the print () function to display the paragraph.
- Various print () functions display ending text, such as corporate guidance and manager names.