Hacking Secret Ciphers with Python

(Ann) #1

62 http://inventwithpython.com/hacking


Email questions to the author: [email protected]


Just remember that every expression with comparison operators always evaluates to the value
True or the value False.


Conditions


A condition is another name for an expression when it is used in a while or if statement. (if
statements aren’t used in the reverse cipher program, but will be covered in the next chapter.)
Conditions usually have comparison operators, but conditions are still just expressions.


Blocks


A block is one or more lines of code grouped together with the same minimum amount of
indentation (that is, the number of spaces in front of the line). You can tell where a block begins
and ends by looking at the line’s indentation.


A block begins when a line is indented by four spaces. Any following line that is also indented by
at least four spaces is part of the block. When a line is indented with another four spaces (for a
total of eight spaces in front of the line), a new block begins inside the block. A block ends when
there is a line of code with the same indentation before the block started.


Let’s look at some imaginary code (it doesn’t matter what the code is, we are only paying
attention to the indentation of each line). We will replace the indenting spaces with black squares
to make them easier to count:



  1. codecodecodecodecodecodecode # zero spaces of indentation

  2. ▪▪▪▪codecodecodecodecodecodecodecodecode # four spaces of indentation

  3. ▪▪▪▪codecodecodecodecodecodecode # four spaces of indentation

  4. ▪▪▪▪▪▪▪▪codecodecodecodecodecodecodecodecode # eight spaces of indentation

  5. ▪▪▪▪codecodecodecodecode # four spaces of indentation



  6. ▪▪▪▪codecodecodecodecodecode # four spaces of indentation

  7. codecodecodecodecodecodecodecodecodecodecode # zero spaces of indentation


You can see that line 1 has no indentation, that is, there are zero spaces in front of the line of
code. But line 2 has four spaces of indentation. Because this is a larger amount of indentation
than the previous line, we know a new block has begun. Line 3 also has four spaces of
indentation, so we know the block continues on line 3.


Line 4 has even more indentation (8 spaces), so a new block has begun. This block is inside the
other blocks. In Python, you can have blocks-within-blocks.

Free download pdf