Hacking Secret Ciphers with Python

(Ann) #1

176 http://inventwithpython.com/hacking


Email questions to the author: [email protected]


['hello', 'world', 'how', 'areXXyou?']








detectEnglish.py


  1. for word in dictionaryFile.read().split('\n'):


Line 16 is a for loop that will set the word variable to each value in the list
dictionaryFile.read().split('\n'). Let’s break this expression down.
dictionaryFile is the variable that stores the file object of the opened file. The
dictionaryFile.read() method call will read the entire file and return it as a very large
string value. On this string, we will call the split() method and split on newline characters.
This split() call will return a list value made up of each word in the dictionary file (because
the dictionary file has one word per line.)


This is why the expression dictionaryFile.read().split('\n') will evaluate to a
list of string values. Since the dictionary text file has one word on each line, the strings in the list
that split() returns will each have one word.


The None Value


None is a special value that you can assign to a variable. The None value represents the lack of
a value. None is the only value of the data type NoneType. (Just like how the Boolean data type
has only two values, the NoneType data type has only one value, None.) It can be very useful to
use the None value when you need a value that means “does not exist”. The None value is
always written without quotes and with a capital “N” and lowercase “one”.


For example, say you had a variable named quizAnswer which holds the user's answer to some
True-False pop quiz question. You could set quizAnswer to None if the user skipped the
question and did not answer it. Using None would be better because if you set it to True or
False before assigning the value of the user's answer, it may look like the user gave an answer
for the question even though they didn't.


Calls to functions that do not return anything (that is, they exit by reaching the end of the function
and not from a return statement) will evaluate to None.


detectEnglish.py
Free download pdf