Arithmetic Operations with Numbers
To use that input as an integer in arithmetic, pass the input() call to int().
From Fixed Values to User Input
A program that always adds the same hard-coded values is limited. A more useful calculator can ask the user for a number, receive the response, and then use that response in an arithmetic calculation. Python's input() function provides the pause-and-receive step needed for this interaction.
input() pauses the program, waits for keyboard input, and returns what the user typed as a string.
The Waiting Point
Execution does not continue immediately after Python reaches input(). First, the prompt is displayed. Then the program waits while the user types. After the user presses Enter, input() returns the typed text, and the program continues with the next operation.
Capturing a Response
Store the result of input() by assigning it to a variable. A prompt string should be supplied so the user knows what to enter. In the source example, the prompt asks, "What is your name?" When the user types Alice and presses Enter, the returned string is stored in the variable name.
If the user types Alice and presses Enter, the value stored in name is the string "Alice".Give input() a clear prompt instead of calling it without an argument. The prompt appears before the program pauses and guides the user about what to enter.
Turning Text into a Number
input() always returns a string, even when the user types digits. Therefore, input such as 15 is returned as the string "15". To use that input as an integer in arithmetic, pass the input() call to int(). The conversion changes the returned value from text to the integer 15.
Read a Number and Add One
Capture a user's number and make it available for an arithmetic calculation.
- 1Display a prompt
input()displays the prompt "Enter a number: " before waiting for the user's response. - 2Receive the response
After the user types a value and presses Enter,
input()returns the typed value as a string. - 3Convert immediately
int()wraps theinput()call, so the returned string is converted to an integer as the value is assigned to number. - 4Use the integer
The resulting integer can be used in arithmetic, such as adding 1 to the value.
The variable number contains an integer rather than the original string returned by input().
Common Input Mistakes
Common mistake Treating input() as if it returned an integer automatically
What it looks likevalue = input("Enter a number: ")Why it failsinput()always returns a string, even when the user types numbers.Do this insteadUse value = int(input("Enter a number: ")) when the response must be an integer for arithmetic.
Common mistake Calling input() without explaining what to enter
What it looks likevalue = input()Why it failsThe user is not given a prompt describing the expected input.
Do this insteadPass a prompt string, such as input("Enter a number: ").
Common mistake Expecting the program to continue before the user presses Enter
What it looks likeThe line after input() does not run while input() is still waiting.
Why it failsinput()pauses program execution until the user types and presses Enter.Do this insteadType the response and press Enter so input() can return the string and execution can resume.
Try the Interaction
What do you think happens?
A program reaches number = int(input("Enter a number: ")). What happens before the next line can use number?
Reveal answer
The program waits for the user to type a response and press Enter.
input() pauses execution and returns the typed response only after the user presses Enter. The surrounding int() call then converts that returned string to an integer.
Describe the stages of number = int(input("Enter a number: ")) in order. Include the prompt, the pause, the returned string, and the conversion to an integer.
Hints
- Start with what the user sees.
- Identify which function waits for keyboard input.
- Remember that
input()returns a string beforeint()converts it.
Key Takeaways
- 1
input()displays a prompt when one is provided, pauses execution, and waits for the user to press Enter. - 2The value returned by
input()is always a string. - 3Assigning
input()to a variable stores the user's response for later use. - 4Use
int(input("prompt"))to convert the user's string response into an integer immediately. - 5After conversion, the integer can be used in an arithmetic calculation.
At a glance
input()pauses a program until the user types a response and presses Enter.- A prompt string tells the user what to enter before the program waits.
input()returns text, including when the user types digits.int()converts that returned text into an integer for arithmetic.- The concise pattern
int(input("prompt"))captures and converts numeric input in one line.