12 Ransom: Randomly capitalizing text

 

All this hard work writing code is getting on my nerves. I’m ready to turn to a life of crime! I’ve kidnapped (cat-napped?) the neighbor’s cat, and I want to send them a ransom note. In the good old days, I’d cut letters from magazines and paste them onto a piece of paper to spell out my demands. That sounds like too much work. Instead, I’m going to write a Python program called ransom.py that will encode text into randomly capitalized letters:

$ ./ransom.py 'give us 2 million dollars or the cat gets it!'
gIVe US 2 milLION DoLlArs or ThE cAt GEts It!

As you can see, my diabolical program accepts the heinous input text as a positional argument. Since this program uses the random module, I want to accept an -s or --seed option so I can replicate the vile output:

$ ./ransom.py --seed 3 'give us 2 million dollars or the cat gets it!'
giVE uS 2 MILlioN dolLaRS OR tHe cAt GETS It!

The dastardly positional argument might name a vicious file, in which case that should be read for the demoniac input text:

$ ./ransom.py --seed 2 ../inputs/fox.txt
the qUIck BROWN fOX JUmps ovEr ThE LAZY DOg.

If the unlawful program is run with no arguments, it should print a short, infernal usage statement:

$ ./ransom.py
usage: ransom.py [-h] [-s int] text
ransom.py: error: the following arguments are required: text

If the nefarious program is run with -h or --help flags, it should print a longer, fiendish usage:

12.1 Writing ransom.py

12.1.1 Mutating the text

12.1.2 Flipping a coin

12.1.3 Creating a new string

12.2 Solution

12.3 Discussion

12.3.1 Iterating through elements in a sequence

12.3.2 Writing a function to choose the letter

12.3.3 Another way to write list.append()

12.3.4 Using a str instead of a list

12.4 Comparing methods