Showing output and reading input¶
~15 min ยท prerequisite: Installing and running Python
What you will be able to do¶
- Show text and numbers on the screen with
print, and control the spacing between them. - Ask the user a question with
inputand store the answer in a variable. - Convert the text from
inputinto a number before you do maths with it.
The idea¶
A program needs two directions of communication. It must output results to the person running it, and it must read input, which is information the person types. In Python, the function print does the output, and the function input does the reading.
print takes any number of values, shows them on one line, and then moves to a new line. It puts a single space between values by default. You can change that space with the sep argument, and you can change the ending with end. An argument is a value you pass to a function inside its parentheses. sep and end are named arguments, so you write them as sep=" " and end="\n".
input shows an optional message, waits until the person types a line and presses Enter, and then gives back what they typed. The result is always a string, which is text, even if the person typed digits. The text "12" and the number 12 look alike on the screen, but Python treats them differently. Lesson 4 explains types in detail. For now, remember that input always returns text.
If you want to do arithmetic on the answer, you must convert it. The function int turns text such as "12" into the integer 12. The function float turns text such as "2.5" into a decimal number. If the text is not a valid number, the conversion fails, and the program stops with an error.
Two important points for this lesson. First, input blocks: the program stops and waits until the person answers. Second, the code examples that use input cannot run inside this course's browser runner, so they appear as plain code blocks. Run them in your terminal, where you can type an answer.
Worked example¶
Save this as greet.py and run it with python3 greet.py.
Line by line, this is what Python does:
input("What is your name? ")shows the question on the screen and waits. The space at the end of the question keeps the cursor from touching the text you type.- When you type a name and press Enter,
inputreturns that name as a string, and the assignment stores it in the variablename. print("Hello,", name)receives two values, the text"Hello,"and the value ofname. It shows them separated by one space.
If you type Ada and press Enter, the output is:
Now a program that does arithmetic on the answer.
The line age = int(age_text) converts the text into an integer. Without that step, age_text + 1 would fail, because you cannot add a number to text.
Try it¶
The blocks below do not wait for a person, so they can run in the browser. Each one captures what print writes, using a helper from the standard library, and checks it with assert.
This block changes the separator and the ending.
This block shows the conversion step without a real question. In a real program, the string would come from input.
Common mistakes¶
-
Forgetting the quotes around text. Writing
print(Hello)tells Python to look for a variable namedHello:Write
print("Hello")instead. -
Adding a number to the result of
input. The following code fails when the person types a number:Convert first with
age = int(input("Age? ")). -
Converting text that is not a number.
int("twelve")gives:The conversion needs digits only, such as
"12". -
Misspelling the function name. Writing
prnt("Hi")gives:Python sometimes adds a hint. Read the hint, then correct the spelling.
-
Leaving out a closing bracket. Writing
print("Hi"gives:
Practice¶
- Write a program that prints your favorite word, then prints the number 7 on the next line.
- Write a program that prints
a,b, andcon one line, separated by a comma and a space, so the output isa, b, c. Hint: usesep. - Write a program that asks for a name with
inputand printsWelcome,followed by that name. Run it and type your name. - Write a program that asks for a number, converts it with
int, and prints the number multiplied by 3. Test it with4. - Stretch: write a program that asks for two numbers as text, converts both to integers, and prints their sum on one line in the form
Sum: 9when the answers are4and5.
Solution
-
python print("tea") print(7) -
python print("a", "b", "c", sep=", ") -
python name = input("Name? ") print("Welcome,", name)
The output keeps the comma after Welcome because print adds one space between the two values.
python number = int(input("Number? ")) print(number * 3)
With the answer 4, the output is 12.
python first = int(input("First number? ")) second = int(input("Second number? ")) print("Sum:", first + second)
With the answers 4 and 5, the output is Sum: 9.
Recap¶
printshows values on one line and moves to a new line. Usesepandendto change the spacing.inputwaits for the person to type, then returns the answer as a string.- Use
intorfloatto turn text into a number before you do maths with it.