Chapter 5: Making Your Program Interactive
Now that we’ve covered the basics of variables, let us write a program
that makes use of them. We’ll revisit the “Hello World” program we wrote
in Chapter 2, but this time we’ll make it interactive. Instead of just saying
hello to the world, we want the world to know our names and ages too. In
order to do that, our program needs to be able to prompt us for
information and display them on the screen.
Two built-in functions can do that for us:
input()
and
print()
.
For now, let’s type the following program in IDLE. Save it and run it.
myName = input("Please enter your name: ")
myAge = input("What about your age: ")
print ("Hello World, my name is", myName, "and I am",
myAge, "years old.")
The program should prompt you for your name.
Please enter your name:
Supposed you entered James. Now press Enter and it’ll prompt you for
your age.
What about your age:
Say you keyed in 20. Now press Enter again. You should get the
following statement:
Hello World, my name is James and I am 20 years old.
Input()
In the example above, we used the
input()
function twice to get our
user’s name and age.
myName = input("Please enter your name: ")
The string “Please enter your name: ” is the prompt that will be displayed
on the screen to give instructions to the user. After the user enters the
relevant information, this information is stored
as a string
in the variable
myName
. The next input statement prompts the user for his age and
stores the information
as a string
in the variable
myAge
.
The
input()
function differs slightly in Python 2 and Python 3. In Python
2, if you want to accept user input as a string, you have to use the
raw_input()
function instead.
Print()
The
print()
function is used to display information to users. It accepts
zero or more expressions as parameters, separated by commas.
In the statement below, we passed 5 parameters to the
print()
function. Can you identify them?
print ("Hello World, my name is", myName, "and I am",
myAge, "years old.")
The first is the string
”Hello World, my name is”
The next is the variable
myName
declared using the input function earlier.
Next is the string
“and I am”
, followed by the variable
myAge
and
finally the string
“years old.”
.
Note that we do not use quotation marks when referring to the variables
myName
and
myAge
. If you use quotation marks, you’ll get the output
Hello World, my name is myName and I am myAge years
old.
instead, which is obviously not what we want.
Another way to print a statement with variables is to use the % formatter
we learned in Chapter 4. To achieve the same output as the first print
statement above, we can write
print ("Hello World, my name is %s and I am %s years
old." %(myName, myAge))
Finally, to print the same statement using the
format()
method, we
write
print (“Hello World, my name is {} and I am {} years
old”.format(myName, myAge))
The
print()
function is another function that differs in Python 2 and
Python 3. In Python 2, you’ll write it without brackets, like this:
print "Hello World, my name is " + myName + " and I am
" + myAge + " years old."
Do'stlaringiz bilan baham: |