Hacking Secret Ciphers with Python

(Ann) #1

78 http://inventwithpython.com/hacking


Email questions to the author: [email protected]


... print('The letter is ' + letter)
... i = i + 1


The letter is H
The letter is o
The letter is w
The letter is d
The letter is y








Notice that this while loop does the exact same thing that the for loop does, but is not as short
and simple as the for loop.


Before we can understand lines 26 to 32 of the Caesar cipher program, we need to first learn
about the if, elif, and else statements, the in and not in operators, and the find()
string method.


Practice Exercises, Chapter 6, Set B


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


The if Statement


An if statement can be read as “If this condition is True, execute the code in the following
block. Otherwise if it is False, skip the block.” Open the file editor and type in the following
small program. Then save the file as password.py and press F5 to run it.


Source code for password.py



  1. print('What is the password?')

  2. password = input()

  3. if password == 'rosebud':

  4. print('Access granted.')

  5. if password != 'rosebud':

  6. print('Access denied.')

  7. print('Done.')


When the password = input() line is executed, the user can type in anything she wants and
it will be stored as a string in the variable password. If she typed in “rosebud” (in all lowercase
letters), then the expression password == 'rosebud' will evaluate to True and the
program execution will enter the following block to print the 'Access granted.' string.


If password == 'rosebud' is False, then this block of code is skipped. Next, the second
if statement will have its condition also evaluated. If this condition, password !=

Free download pdf