Mod02 Getting Inputs
Mod02 Getting Inputs
Checkpoint
2.10 What is a variable?
2.11 Which of the following are illegal variable names in Python, and why?
x
99bottles
july2009
theSalesFigureForFiscalYear
r&d
grade_report
2.12 Is the variable name Sales the same as sales? Why or why not?
2.13 Is the following assignment statement valid or invalid? If it is invalid, why?
72 = amount
2.14 What will the following code display?
val = 99
print('The value is', 'val')
2.15 Look at the following assignment statements:
value1 = 99
value2 = 45.9
value3 = 7.0
value4 = 7
value5 = 'abc'
After these statements execute, what is the Python data type of the values
referenced by each variable?
2.16 What will be displayed by the following program?
my_value = 99
my_value = 0
print(my_value)
Most of the programs that you will write will need to read input and then perform an
operation on that input. In this section, we will discuss a basic input operation: reading
VideoNote
Reading Input
data that has been typed on the keyboard. When a program reads data from the keyboard,
from the usually it stores that data in a variable so it can be used later by the program.
Keyboard
In this book, we use Python’s built-in input function to read input from the keyboard. The
input function reads a piece of data that has been entered at the keyboard and returns that
piece of data, as a string, back to the program. You normally use the input function in an
assignment statement that follows this general format:
variable = input(prompt)
72 Chapter 2 Input, Processing, and Output
In the general format, prompt is a string that is displayed on the screen. The string’s pur-
pose is to instruct the user to enter a value; variable is the name of a variable that ref-
erences the data that was entered on the keyboard. Here is an example of a statement that
uses the input function to read data from the keyboard:
name = input('What is your name? ')
When the first statement was entered, the interpreter displayed the prompt 'What is your
name? ' and waited for the user to enter some data. The user entered Holly and pressed
the Enter key. As a result, the string 'Holly' was assigned to the name variable. When the
second statement was entered, the interpreter displayed the value referenced by the name
variable.
Program 2-12 shows a complete program that uses the input function to read two strings
as input from the keyboard.
Notice the last character in the string, inside the quote marks, is a space. The same is true
for the following string, used as prompt in line 5:
'Enter your last name: '
We put a space character at the end of each string because the input function does not
automatically display a space after the prompt. When the user begins typing characters,
they appear on the screen immediately after the prompt. Making the last character in the
prompt a space visually separates the prompt from the user’s input on the screen.
For example, suppose you are writing a payroll program and you want to get the number
of hours that the user has worked. Look at the following code:
string_value = input('How many hours did you work? ')
hours = int(string_value)
The first statement gets the number of hours from the user and assigns that value as a string
to the string_value variable. The second statement calls the int() function, passing
string_value as an argument. The value referenced by string_value is converted to an
int and assigned to the hours variable.
This example illustrates how the int() function works, but it is inefficient because it cre-
ates two variables: one to hold the string that is returned from the input function, and
another to hold the integer that is returned from the int() function. The following code
shows a better approach. This one statement does all the work that the previously shown
two statements do, and it creates only one variable:
hours = int(input('How many hours did you work? '))
74 Chapter 2 Input, Processing, and Output
This one statement uses nested function calls. The value that is returned from the input
function is passed as an argument to the int() function. This is how it works:
• It calls the input function to get a value entered at the keyboard.
• The value that is returned from the input function (a string) is passed as an argument
to the int() function.
• The int value that is returned from the int() function is assigned to the hours variable.
After this statement executes, the hours variable is assigned the value entered at the key-
board, converted to an int.
Let’s look at another example. Suppose you want to get the user’s hourly pay rate. The fol-
lowing statement prompts the user to enter that value at the keyboard, converts the value
to a float, and assigns it to the pay_rate variable:
pay_rate = float(input('What is your hourly pay rate? '))
After this statement executes, the pay_rate variable is assigned the value entered at the
keyboard, converted to a float.
Program 2-13 shows a complete program that uses the input function to read a string, an
int, and a float, as input from the keyboard.
NOTE: In this section, we mentioned the user. The user is simply any hypothetical
person that is using a program and providing input for it. The user is sometimes called
the end user.
Checkpoint
2.17 You need the user of a program to enter a customer’s last name. Write a statement
that prompts the user to enter this data and assigns the input to a variable.
2.18 You need the user of a program to enter the amount of sales for the week. Write a
statement that prompts the user to enter this data and assigns the input to a variable.
The values on the right and left of the + operator are called operands. These are values that
the + operator adds together. If you type this expression in interactive mode, you will see
that it gives the value 14:
>>> 12 + 2 Enter
14
>>>
Variables may also be used in a math expression. For example, suppose we have two variables
named hours and pay_rate. The following math expression uses the * operator to multiply
the value referenced by the hours variable by the value referenced by the pay_rate variable:
hours * pay_rate
When we use a math expression to calculate a value, normally we want to save that value
in memory so we can use it again in the program. We do this with an assignment statement.
Program 2-14 shows an example.
Program Output
Your pay is 3700.0