Building Programs with Variables, Expressions, and Statements
input() pauses program execution and waits for keyboard input from the user.
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
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)
AliceFollowing 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
Assuming input() returns a number when the user types digits.
input() returns the user's entry as a string regardless of what they type.
Fix:
Use int() or float() before performing arithmetic.Using input() without assigning its result to a variable.
The returned string is not available through a named variable for later use.
Fix:
Assign the result, as in name = input().Expecting statements after input() to run before the user responds.
input() pauses execution at that point.
Fix:
Trace 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
- input() pauses program execution and waits for keyboard input.
- The function returns the user's entry as a string, even when the entry looks like a number.
- Assigning input() to a variable lets later statements use the returned data.
- With multiple input() calls, execution reaches them in program order and pauses at each one.
- Convert string input with int() or float() before performing arithmetic.
Key Takeaways
- 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() or float() to convert string input before arithmetic.