Using print() to Display Output
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 inside its code, but real programs also 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 input() function for this kind of interaction. Although print() displays a value, input() brings data into the program.
The central behavior is simple: input() pauses the program, waits for keyboard input, and then returns what the user entered as a string.
The Pause at input()
What do you think happens?
What happens when this program reaches input() and the user types Alice before pressing Enter?
Reveal answer
Answer: The program pauses, waits for the entry, and then continues.
The program reaches input() and stops at that point while the cursor waits for keyboard input. After the user types Alice and presses Enter, input() returns the string Alice, and execution moves to the next statement.
The important point is that later statements do not run while input() is waiting. If a program contains a print() call after input(), that print() call happens only after the user has entered data and pressed Enter.
Storing the Returned String
The usual pattern is to assign the result of input() to a variable. In name = input(), input() reads from the keyboard and returns the entered data. The assignment operator stores that returned string in the variable name. The variable can then be used by later statements in the program.
Tracing a Name Through the Program
Follow the program when the user types Alice and presses Enter.
Reach the first statement: The program reaches input() and pauses while it waits for keyboard input.
Submit the entry: The user types Alice and presses Enter. input() returns the string Alice.
Assign the value: The assignment stores the returned string in the variable name.
Run the next statement: Execution moves to print(name), which displays the value stored in name.
The displayed value is Alice. Nothing from the print() statement is displayed until after the input has been provided.
Strings Even When Input Looks Numeric
input() always returns a string, regardless of what the user types. If the user types 25, the returned value is the string '25', not the integer 25. This distinction matters because strings and integers behave differently in a program.
Order of Multiple Inputs
Each input() call runs when execution reaches that call. If one input() statement appears before another, the first one pauses the program first. After the first entry is submitted, execution continues until it reaches the next input() call, where it pauses again.
In this example, the program waits at the first input() call. It cannot reach the second input() call until the first entry has been submitted. It then waits again at the second call. Only after both values have been collected can the two print() statements run.
Mistakes with Collected Data
Expecting input() to continue immediately
Pausing is the intended behavior. input() waits for keyboard input before execution continues.
Fix:
Type a value and press Enter, then observe the next statement run.Using input() without storing its result
The entered data is returned as a string, but without assignment there is no named variable available for later use.
Fix:
Assign the result to a variable, such as name = input().Treating numeric-looking input as an integer automatically
Even when the user types 25, input() returns the string '25'.
Fix:
Convert the returned string with int() or float() before performing arithmetic.Expecting print() to run before input() is answered
Statements after the waiting input() call do not run until the user submits an entry.
Fix:
Trace the statements in order and remember that execution resumes only after input is provided.
Testing Interactive Programs
After writing a program that uses input(), run it and type different entries. Check that the program pauses where the input() call occurs, resumes after you submit the entry, and uses the exact text you entered in later output. Testing different entries helps you verify both the execution flow and the data passed through the variable.
Write a short program that collects a user's name with input(), stores it in a variable, and then displays that variable with print(). Before running it, identify the exact line where the program will pause.
Hints
- Assign input() to a variable.
- The print() statement should use that variable.
- The pause occurs when execution reaches input().
Key Takeaways
- input() pauses program execution and waits for keyboard input.
- After the user submits an entry, input() returns that entry as a string.
- Assign the returned value to a variable so later statements can use it.
- Use int() or float() to convert string input before performing arithmetic.
- print() displays stored data, while input() collects data and controls when execution resumes.
Key Takeaways
- input() creates a pause in the program while it waits for keyboard input.
- Every value returned by input() is a string, even when the entry looks like a number.
- Assign input() to a variable to preserve and reuse the entered data.
- Statements run in order, so later input() and print() calls wait until earlier input has been submitted.
- Convert string input with int() or float() before performing arithmetic.