Arithmetic Operations with Numbers
input() pauses your program and waits for the user to type something on their keyboard, then returns what they typed as a string.
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.
Display a prompt: input() displays the prompt "Enter a number: " before waiting for the user's response.
Receive the response: After the user types a value and presses Enter, input() returns the typed value as a string.
Convert immediately: int() wraps the input() call, so the returned string is converted to an integer as the value is assigned to number.
Use 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
Treating input() as if it returned an integer automatically
input() always returns a string, even when the user types numbers.
Fix:
Use value = int(input("Enter a number: ")) when the response must be an integer for arithmetic.Calling input() without explaining what to enter
The user is not given a prompt describing the expected input.
Fix:
Pass a prompt string, such as input("Enter a number: ").Expecting the program to continue before the user presses Enter
input() pauses program execution until the user types and presses Enter.
Fix:
Type 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
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 before int() converts it.
Key Takeaways
- input() displays a prompt when one is provided, pauses execution, and waits for the user to press Enter.
- The value returned by input() is always a string.
- Assigning input() to a variable stores the user's response for later use.
- Use int(input("prompt")) to convert the user's string response into an integer immediately.
- After conversion, the integer can be used in an arithmetic calculation.
Key Takeaways
- 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.