Hacking Secret Ciphers with Python

(Ann) #1

52 http://inventwithpython.com/hacking


Email questions to the author: [email protected]


same thing for function calls as “evaluates”.) In this case, the return value of the input()
function is the string that the user typed in-their name. If the user typed in Albert, the input()
function call evaluates (that is, returns) to the string 'Albert'.


The function named input() does not need any arguments (unlike the print() function),
which is why there is nothing in between the parentheses.


hello.py


  1. print('It is good to meet you, ' + myName)


For line 5’s print() call, we use the plus operator (+) to concatenate the string 'It is
good to meet you, ' and the string stored in the myName variable, which is the name that
our user input into the program. This is how we get the program to greet us by name.


Ending the Program


Once the program executes the last line, it stops. At this point it has terminated or exited and
all of the variables are forgotten by the computer, including the string we stored in myName. If
you try running the program again and typing a different name it will print that name.


Hello world!
What is your name?
Alan
It is good to meet you, Alan


Remember, the computer only does exactly what you program it to do. In this program it is
programmed to ask you for your name, let you type in a string, and then say hello and display the
string you typed.


But computers are dumb. The program doesn’t care if you type in your name, someone else’s
name, or just something silly. You can type in anything you want and the computer will treat it
the same way:


Hello world!
What is your name?
poop
It is good to meet you, poop


Practice Exercises, Chapter 4, Set C


Practice exercises can be found at http://invpy.com/hackingpractice 4 C.

Free download pdf