Building Programs with Variables, Expressions, and Statements
The important change is that the program no longer has all of its data before it starts. At an input() call, execution waits for the user before continuing.
From Fixed Data to User Data
A program can use values written directly in its code, but useful programs often need information from the person running them. A banking app may ask for an account number, a game may ask for a name, and a calculator may wait for numbers. Python provides the built-in function input() for this kind of interaction. It pauses program execution, waits for keyboard input, and then gives the program the user's entry.
The important change is that the program no longer has all of its data before it starts. At an input() call, execution waits for the user before continuing.
The Waiting Point
What do you think happens?
What happens when this program reaches input() and the user has not typed anything yet?
Reveal answer
The program pauses and waits for keyboard input.
When execution reaches input(), the program stops at that point. The cursor waits for the user, and execution continues only after the user types a response and presses Enter.
The diagram shows a pause, not a permanent end. The program reaches input(), waits while the user responds, receives the typed entry as a string, and then moves to the next statement. Nothing after that input() call runs until the response has been provided.
Saving the Returned String
Writing name = input() creates a pipeline. First, input() reads from the keyboard. Next, it returns what the user typed as a string. Finally, the assignment operator stores that string in the variable name. From then on, name holds the returned value and later statements can use it.
name = input()
print(name)- 1Execution reaches the first line and pauses at
input(). - 2The user types Alice and presses Enter.
- 3
input()returns the string Alice, and assignment stores it in name. - 4Execution moves to the second line, which prints Alice.
- The first statement calls
input()and waits. - After the user types Alice and presses Enter, the returned string is assigned to name.
- The second statement prints the value stored in name.
AliceAliceFollowing Multiple Input Calls
A program can contain more than one input() call. Execution reaches the first call in the program's order and waits there. After the user responds, the program continues with the statements between that call and the next one. When execution reaches the next input() call, it pauses again. This makes the locations of input() calls important when you trace a program.
To trace such a program, mark every input() call and read from top to bottom. At each marked point, ask what the user types, what string is returned, which variable receives it, and which statements run before the next input() call.
Strings That Look Like Numbers
input() always returns a string, regardless of what the user types. If a user types 25, the returned value is the string '25', not the integer 25. The characters may look like a number, but the value is still string data. Strings and integers behave differently, so arithmetic requires conversion with int() or float() first.
Mistakes with User Input
Common mistake Assuming input() returns a number when the user types digits.
What it looks likeA user types 25, and the program treats the returned value as the integer 25.
Why it failsinput()returns the user's entry as a string regardless of what they type.Do this insteadUse int() or float() before performing arithmetic.Common mistake Using input() without assigning its result to a variable.
What it looks likeThe program reads a response but does not store it for a later statement.
Why it failsThe returned string is not available through a named variable for later use.
Do this insteadAssign the result, as in name = input().Common mistake Expecting statements after input() to run before the user responds.
What it looks likeA learner expects the next output to appear while input() is still waiting.
Why it failsinput()pauses execution at that point.Do this insteadTrace the pause first, then continue tracing after the user types and presses Enter.
Practice the Execution Trace
Trace this program on paper. Identify where execution pauses, what value is stored in each variable, and which statement runs after the second response. Assume the user types Alice at the first input() call and 25 at the second.
Hints
- Read the statements from top to bottom.
- Remember that both responses are returned as strings.
- Mark the two places where execution waits.
Test an input-based program by running it and typing different inputs. Verify that the program pauses and waits, that each input call is reached in order, and that the output includes the exact text entered.
Key Takeaways
- 1
input()pauses program execution and waits for keyboard input. - 2The function returns the user's entry as a string, even when the entry looks like a number.
- 3Assigning
input()to a variable lets later statements use the returned data. - 4With multiple
input()calls, execution reaches them in program order and pauses at each one. - 5Convert string input with
int()orfloat()before performing arithmetic.
At a glance
input()creates a waiting point where a program listens for keyboard input.- After the user responds,
input()returns the entry as a string and execution continues. - Store the returned string in a variable so later statements can use it.
- Trace multiple
input()calls from top to bottom, noting what runs between them. - Use
int()orfloat()to convert string input before arithmetic.