Getting User Input


After appreciating all that program Greeter has to offer, you may still be thinking, "So what?" Yes, you could write a program that does exactly what Greeter does without going to the trouble of creating any fancy variables. But to do fundamentally important things, including getting, storing, and manipulating user input, you need variables. Check out the next program, which uses input to give a personalized greeting.

Introducing the Personal Greeter Program

The Personal Greeter program adds a single, but very cool, element to the Greeter program: user input. Instead of working with a predefined value, the computer lets the user enter his or her name and then uses it to say Hi. Figure 2.7 shows off the program.

click to expand
Figure 2.7: Now, name is assigned a string based on whatever the user enters, including "Rupert".

Getting user input isn't very hard. As a result, the code doesn't look much different:

 # Personal Greeter # Demonstrates getting user input # Michael Dawson 1/13/03 name = raw_input("Hi. What's your name? ") print name print "Hi, " + name raw_input("\n\nPress the enter key to exit.") 

Using the raw_input() Function

The only line that's changed is the assignment statement:

 name = raw_input("Hi. What's your name? ") 

The left side of the statement is exactly the same as in the Greeter program. name is created and a value is assigned to it, just like before. But this time, the value isn't a string I supply. It's the string value of whatever the user enters.

On the right side of the assignment statement is a call to the function raw_input(). A function is like a mini-program that goes off and does some specific task. The task of raw_input() is to get some text from the user. Sometimes you give a function values to use. You put these values, called arguments, between the parentheses. In this case, the one argument passed to raw_input() is the string "Hi. What's your name? ". As you can see from Figure 2.7, raw_input() uses the string to prompt the user. raw_input() waits for the user to enter something. Once the user presses the Enter key, raw_input() returns whatever the user typed, as a string. That's the string that name gets.

If you're still not totally clear on how this works, think of it this way: using raw_input() is like ordering a pizza. The raw_input() function is like a pizza parlor. You make a call to a pizza parlor to place your order, and you make a call to the raw_input() function to kick it into gear. When you call the pizza parlor, you provide information, like "pepperoni". When you call the raw_input() function, you pass it the argument, "Hi. What's your name?". After you finish your call to the pizza parlor, the employees get a pepperoni pizza to your door. And after you make your call to raw_input(), the function returns whatever string the user entered.

The rest of the Personal Greeter program works just like the Greeter program. It makes no difference to the computer how name gets its value. So the line

 print name 

prints the value of name. While the line

 print "Hi, " + name 

concatenates the "Hi," and the value of name, and prints this new string out. At this point, you know enough to understand the last line in all of these console programs. The goal of the last line is to wait for the user to press the Enter key:

 raw_input("\n\nPress the enter key to exit.") 

It does exactly that through the raw_input() function. Since I don't care what the user enters, so long as he or she presses the Enter key, I don't assign the return value of raw_input() to a variable like before. It may seem weird to get a value and do nothing with it, but it's my option. If I don't assign the return value to a variable, the computer just ignores it. So once the user presses the Enter key, the program ends and the console window closes.




Python Programming for the Absolute Beginner
Python Programming for the Absolute Beginner, 3rd Edition
ISBN: 1435455002
EAN: 2147483647
Year: 2003
Pages: 194

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net