
C H A P T E R 1 2
A N I N T R O D U C T I O N T O P Y T H O N
181
This small line actually achieves quite a lot. The first part,
userName =
, tells Python to cre-
ate a new
variable
—a location for storing a piece of information—called
userName
. The
equals symbol tells Python that the variable should be set to whatever follows. However, in
this case what follows isn’t just a piece of information, but another command:
raw_input
.
This is a tool designed to accept
string
(text) input from the keyboard, and allows for a mes-
sage to be printed to the default output so the user knows what to type. This helps keep the
program simple—without the ability to print a prompt telling the user what to type, a sec-
ond line with a
command would be required. Remember to leave a space at the end of
the prompt; otherwise, the user’s input will begin immediately after the question mark.
WARNING
When asking the user to type in text, always use
raw_input
. This provides security that the
input
command alone does not—if you just use
input
, a user may
inject
his or her own code
into your program and have it crash or work contrary to your intentions.
With the user’s name now stored safely in the
userName
variable, the program can begin to
get clever. Welcome the user using the following line:
print “Welcome to the program,”, userName
This line demonstrates a secondary function of the
command introduced in Example
1: the ability to print out the contents of variables. This
command is split into two:
the first section prints everything between the two quotation marks, and the second comma
tells
that more should be printed to the same line. Simply typing the variable name
userName
is enough for Python to know that it should print the contents of that variable,
resulting in a message customised to the user’s own name.
This example program is going to take the form of a simple but friendly calculator. Unlike
Example 1, it will continue to run until the user tells it otherwise. This is achieved using a
loop, just as in Scratch. Begin the loop by typing the following two lines:
goAgain = 1
while goAgain == 1:
The first line creates a new variable called
goAgain
and sets it to
1
. The second line begins the
loop, and tells Python that
while
the
goAgain
variable is equal to
1
, it should continue to
loop through the following code. As the next few lines are written, they will need to be
indented
by inserting four spaces at the start of each line. These spaces tell Python which lines are part of
the loop and which lines are outside the loop. If you’re using IDLE, the spaces will be inserted
automatically; if you’re using a text editor, remember to insert the spaces manually.
Summary of Contents for A
Page 1: ......
Page 2: ......
Page 3: ...Raspberry Pi User Guide 2nd Edition...
Page 4: ......
Page 5: ...Raspberry Pi User Guide 2nd Edition Eben Upton and Gareth Halfacree...
Page 10: ......
Page 26: ...R A S P B E R R Y P I U S E R G U I D E S E C O N D E D I T I O N 10...
Page 28: ......
Page 29: ...Chapter 1 Meet the Raspberry Pi...
Page 37: ...Chapter 2 Getting Started with the Raspberry Pi...
Page 56: ......
Page 57: ...Chapter 3 Linux System Administration...
Page 79: ...Chapter 4 Troubleshooting...
Page 89: ...Chapter 5 Network Configuration...
Page 109: ...Chapter 6 The Raspberry Pi Software Configuration Tool...
Page 122: ......
Page 123: ...Chapter 7 Advanced Raspberry Pi Configuration...
Page 140: ......
Page 141: ...Chapter 8 The Pi as a Home Theatre PC...
Page 151: ...Chapter 9 The Pi as a Productivity Machine...
Page 160: ......
Page 161: ...Chapter 10 The Pi as a Web Server...
Page 172: ......
Page 173: ...Chapter 11 An Introduction to Scratch...
Page 189: ...Chapter 12 An Introduction to Python...
Page 216: ......
Page 218: ......
Page 219: ...Chapter 13 Learning to Hack Hardware...
Page 234: ......
Page 235: ...Chapter 14 The GPIO Port...
Page 249: ...Chapter 15 The Raspberry Pi Camera Module...
Page 265: ...Chapter 16 Add on Boards...
Page 280: ......
Page 281: ...Appendix A Python Recipes...
Page 287: ...Appendix B Raspberry Pi Camera Module Quick Reference...
Page 293: ...Appendix C HDMI Display Modes...