9 Dial-A-Curse: Generating random insults from lists of words

 

"He or she is a slimy-sided, frog-mouthed, silt-eating slug with the brains of a turtle." — Dial-A-Curse

Random events are at the heart of interesting games and puzzles. Humans quickly grow bored of things that are always the same, so let’s learn how to make our programs more interesting by having them behave differently each time they are run. This exercise will introduce how to randomly select one or more elements from lists of options. To explore randomness, we’ll create a program called abuse.py that will insult the user by randomly selecting adjectives and nouns to create slanderous epithets.

In order to test randomness, though, we need to control it. It turns out that "random" events on computers are rarely actually random but only "pseudo-random," which means we can control them using a "seed."[9] Each time you use the same seed, you get the same "random" choices!

Shakespeare had some of the best insults, so we’ll draw from the vocabulary of his works. Here is the list of adjectives you should use:

bankrupt base caterwauling corrupt cullionly detestable dishonest false filthsome filthy foolish foul gross heedless indistinguishable infected insatiate irksome lascivious lecherous loathsome lubbery old peevish rascaly rotten ruinous scurilous scurvy slanderous sodden-witted thin-faced toad-spotted unmannered vile wall-eyed

And these are the nouns:

9.1  Writing abuse.py

 
 
 

9.1.1  Validating arguments

 
 

9.1.2  Importing and seeding the random module

 
 
 

9.1.3  Defining the adjectives and nouns

 
 
 

9.1.4  Taking random samples and choices

 

9.1.5  Formatting the output

 
 
 

9.2  Solution

 
 

9.3  Discussion

 
 
 

9.3.1  Defining the arguments

 
 

9.3.2  Using parser.error()

 
 
 
 

9.3.3  Program exit values and STDERR

 

9.3.4  Controlling randomness with random.seed()

 

9.3.5  Iterating for loops with range()

 
 

9.3.6  Constructing the insults

 

9.4  Review

 
 

9.5  Going Further

 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest