Hacking Secret Ciphers with Python

(Ann) #1

180 http://inventwithpython.com/hacking


Email questions to the author: [email protected]








The int() function returns an integer version of its argument, and the str() function returns a
string. Try typing the following into the interactive shell:





float(42)
42.0
int(42.0)
42
int(42.7)
42
int("42")
42
str(42)
'42'
str(42.7)
'42.7'





The float(), int(), and str() functions are helpful if you need a value’s equivalent in a
different data type. But you might be wondering why we pass matches to float() on line 36
in the first place.


The reason is to make our detectEnglish module work with Python 2. Python 2 will do
integer division when both values in the division operation are integers. This means that the result
will be rounded down. So using Python 2, 22 / 7 will evaluate to 3. However, if one of the
values is a float, Python 2 will do regular division: 22.0 / 7 will evaluate to
3.142857142857143. This is why line 36 calls float(). This is called making the code


backwards compatible with previous versions.


Python 3 always does regular division no matter if the values are floats or ints.


Practice Exercises, Chapter 12, Set D


Practice exercises can be found at http://invpy.com/hackingpractice12D.


Back to the Code


detectEnglish.py


  1. def removeNonLetters(message):

  2. lettersOnly = []

  3. for symbol in message:

Free download pdf