6.4.1 – User input and output
Learn to ask for information, store the user's answer and respond clearly. Use text and numeric input in small Python programs, predict an interaction before running it, and build a complete interaction from stated requirements.
Accepting text input
User input is data a person supplies while a program runs. A console program can display a question, wait for a line of keyboard input and then use the answer. A prompt tells the user what to enter.
name = input("Enter your first name: ")
Python evaluates the right-hand side first. input() displays the prompt and waits. When the user presses Enter, it returns the typed line as a string, without the Enter key's newline. Assignment then stores that string in name. The prompt is not part of the stored value. If the user enters Maya, name contains "Maya".
Calling input() without assigning its result still asks for input, but does not leave a variable containing the answer for later statements. To respond using that answer, store it and then refer to the variable:
name = input("Enter your first name: ")
print("Hello, {}.".format(name))
The {} is a placeholder. .format(name) inserts the value stored in name into that position. print() displays the resulting message and normally starts a new line afterwards. It does not ask for an answer.
Greeting Maya
Example runEnter your first name: Maya
Hello, Maya.
In this transcript, Maya on the first line is the user's entry echoed by the console. Hello, Maya. is the program's response. A task asking for the final printed line wants the response, whereas a full interaction includes prompts and entered data too. Quotation marks around Python string literals delimit the string in source; they are not displayed.
Converting numeric input
input() returns text even when the user types digits. The string "14" and the integer 14 represent related information but are different types. A program needs a numeric value for a calculation. Type conversion produces a value of another type; it does not automatically change the original variable.
For whole-number quantities, use int():
age_text = input("Age in whole years: ")
age = int(age_text)
next_age = age + 1
print("On your next birthday you will be {}.".format(next_age))
The addition adds one year; it must use the integer age, not the text age_text. For an entry of 14, the state develops as follows:
| After source line | age_text | age | next_age |
|---|---|---|---|
| 1 | "14" | not assigned | not assigned |
| 2 | "14" | 14 | not assigned |
| 3 | "14" | 14 | 15 |
Next birthday
Example runAge in whole years: 14
On your next birthday you will be 15.
The same input and conversion can be combined. Evaluate the inner call first: obtain the string, convert it, then store the integer.
age = int(input("Age in whole years: "))
For a measurement that may contain a decimal point, use float():
distance = float(input("Distance in km: "))
An entry of 2.5 gives a floating-point value of 2.5. Using int() on the input string "2.5" does not round it: it raises a conversion error. A prompt explains the intended format but cannot force a user to obey it. These examples assume suitable input; checking and rejecting unsuitable entries is taught in validation.
Do not convert text merely because it consists of digits. A reference such as "0042" may need its leading zeros preserved. Convert quantities for numeric processing; keep names and textual identifiers as strings.
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account