User Input and Output
Console Input and Output: Reading Entries and Showing Results
Console Input and Output lets a program communicate with a person through a console. You might enter a name, a measurement, or a quantity, then read a labelled result. The examples here use Python 3. Focus on what the program receives and what it displays: a message on the screen and a value available for calculation serve different purposes.
Reading an entry
Console input is information a running program receives through its text interface. In a normal interactive Python session, input() displays an optional message, waits for an entry, and returns the entered text when you press Enter.
Example: A program asks for a visitor's name and stores the response.
name = input("Your name: ")The message Your name: is a prompt. It appears without automatically moving to a new line, so the user can type beside it. The space after the colon separates the message from the response.
If you type Maya and press Enter, name contains the text Maya. The prompt itself is not included in that value, and neither is the final Enter character. The program then continues to its next instruction. A second input() call waits for a separate entry.
Turning numeric entries into numbers
Python's input() returns text even when the entry contains only digits. For calculations, convert that text using a suitable numeric conversion.
Here, float() converts suitable numeric text into a number that can represent a decimal measurement. Use int() for suitable whole-number text.
Intended entry | Suitable code | Example entry |
|---|---|---|
A person's name | input("Name: ") | Maya |
A whole-number count | int(input("Items: ")) | 8 |
A decimal measurement | float(input("Length (m): ")) | 1.65 |
For the measurement prompt, enter 1.65, without typing m. The prompt supplies the unit for the person; the conversion needs numeric text. These examples assume valid entries. Entering hello where a number is expected causes a conversion error.
Example: These two instructions read a measurement and convert it explicitly.
length_text = input("Length (m): ")
length = float(length_text)Separating the instructions makes the two jobs visible: first receive text, then obtain a number.
Displaying a useful result
Console output is information a program displays through its text interface. Use print() to show a message, a stored value, or both.
print("Length:", length, "m")If length holds 1.65, the displayed line is Length: 1.65 m. By default, print() puts one space between its comma-separated items and finishes with a newline, placing later output on the next line. The quotation marks identify text in the code; they are not displayed.
A label explains the number, and a unit explains its scale. Displaying a value does not change the value stored in the program.
What to remember
- input() receives an entry as text.
- Convert numeric text before using it as a number.
- print() displays values with spaces between items by default.
- Clear prompts, labels, and units make the interaction understandable.
Example: Reporting a Water Container's Volume
A laboratory assistant uses a Python 3 program to report the volume in a container after adding water. Each prompt is a displayed message requesting an entry. Assume the assistant enters valid numbers without units: 2.75 at the first prompt and 1.20 at the second.
initial_text = input("Initial volume (L): ")
added_text = input("Added volume (L): ")
initial = float(initial_text)
added = float(added_text)
total = initial + added
print("Total volume:", total, "L")The calculation assumes the stated added volume increases the container's volume by that amount, with no water lost.
a) Why would adding the two entered text values directly fail to calculate the total volume? State what that text addition would produce.
First follow the two input calls. The first entry supplies the text ________, and the second supplies the text ________. Neither input call includes the prompt or the unit in its returned text.
Next consider adding those unconverted values. With text on both sides, + joins the characters in order rather than calculating a volume. Digits received as text do not automatically become numbers.
The expression initial_text + added_text would produce the text "2.751.20", which is not the total volume.
b) Calculate the total volume after the two conversions. Give the result in litres to two decimal places.
Numeric conversion changes suitable numeric text into a number the program can use in calculation. Here, float() converts each entry separately.
The converted initial value represents ________, and the converted added value represents ________. The variables store numbers; the prompts establish that those numbers represent litres.
Add the volumes using the stated no-loss assumption:
The total volume is 3.95 L. It is larger than the initial volume by exactly the amount added, which checks the calculation.
c) State the exact line produced by the final print instruction. Explain its spacing and unit.
First identify the three items passed to print(): the text "Total volume:", the numeric value in total, and the text "L".
By default, print() inserts one space between adjacent items. It displays this numeric result as 3.95, and it does not display the quotation marks used around the text in the code.
The exact line is Total volume: 3.95 L. The final L is an explicitly printed label rather than a unit stored inside total. After the line, print() adds a newline. The prompts and typed responses occur earlier and are not part of this final line.
An equipment checkout program runs the following Python code. The user types 8042 and presses Enter when input() waits for an entry.
print("Tag", end=": ")
tag = input()
print("Checking", tag, sep=" -> ")
print("Done", end="!")The terminal echo is the console's display of the characters the user types. Exclude that display and the newline caused by the user's Enter key. Count only characters written by the program's calls.
Complete the table with exact integers. In the first answer column, count every letter, digit, space, and punctuation mark, but exclude newline characters. Count program-written newline characters separately in the second answer column.
| Call | Characters excluding newlines | Newline characters |
| First print call | ||
| input call | ||
| Second print call | ||
| Third print call |