Reading input and displaying results
Input lets a person provide a value while a Python program is running.
How it works
Keep input, conversion, and calculation as separate steps when learning. With age text 19, first store the characters, then convert them to an integer, then add one. If conversion fails, you immediately know which step needs attention.
Printing a result lets a person see it. Returning it from a function lets another part of the program decide what to do with it.
Walk through an example
input("Age: ") displays a prompt and returns the line as a string. Convert with int() before adding to an age.
What to watch for
A person can leave the prompt blank or type letters. Check the input before relying on a numeric calculation.
Code example
answer = "19"
print(int(answer) + 1)Check your understanding
What does int("19") + 1 evaluate to?
See the example and explanation
- Worked example
- Given
Input text: 19 - Answer
20
Conversion gives an integer first. Adding one then performs arithmetic.