Input and output
Estimated Time: 30 minutes
Key ideas
- Show output with
print()
- Get data from the user with
input()
Printing Output
Websites and apps you have used in the past have a whole screen full of text and buttons. Eventually, you’ll learn to build those kinds of programs, but we’re starting with the basics: programs that work with text.
print
shows some output to the Console:
print("Hello, world")
You’ve written code like this from your first “Hello, World” program. When you run it, the output shows up.
In Replit, output shows in the ‘Console’ tab. On early computers, there was only a text console. On the earliest computers, there wasn’t a screen at all. Instead, the output was printed out on paper. The output now shows up in the Console in our web browser, but we still call the function print
.
print("We can print any string we want")
x = ("If the string is in a variable, we can print the variable")
print(x) # If the string is in a variable, we can print the variable
Getting Input from the User
You've already used the input()
function in earlier challenges to get the name from the user.
When Python sees input
:
- It prints out the arguments to
input
, similar toprint
. - It pauses to wait for the user to type something in
- It waits for the user to press Enter
- When the user presses Enter,
input
gives the program the text that the user typed in
We can design lots of text-based interactions using input
. A calculator, a search engine, a quiz, a chatbot - all of these and more can be designed to use text input and output.
Converting Inputs
The input()
function returns a string. If you want a number, need to convert the data type using int
or float
.
response = input("How old are you?")
age = int(response)
It’s common to see the input and the conversion all at once, like this:
age = int(input("How old are you?"))
Converting to a float is similar:
soda_price = float(input("How much does a soda cost?"))
What does python do if you try to convert a string like "3.5"
to an integer using int
?
int("3.5") # ValueError: invalid literal for int() with base 10: '3.5'
⚠️ Python doesn’t want to accidentally lose information. Instead of guessing whether you want to round up or down, it raises a ValueError
and halts the program.
Practice: Assigning and printing user input
Experimenting With Print
print()
print
print("Hello", "world")
What happens when there’s nothing between the parentheses? Or if you leave off the parentheses? Or if you put more than one thing between the parentheses?
Sometimes, the best way to find things out about how Python works is to try it out.
Experiment with
Print with multiple arguments
The print()
function can take more than one argument. It can have more than one thing inside the parentheses (...)
However, you must separate arguments by commas.
name = "Emmy"
print("Hello" name) # SyntaxError: invalid syntax (because there's no comma)
print("Hello", name) # Hello Emmy
Example: Input and Output
Here's the code for the exercise:
1. first_num = int(input("enter first number: "))
2. second_num = int(input("enter second number: "))
3.
4. total = first_num + second_num
5.
6. print("the sum is: ", total)
In plain English, here is what the code does:
- line 1: Ask the user for an input. Convert the input to an integer, and store it in a variable named
first_num
- line 2: Ask the user for another input. Convert the input to an integer, and store it in a variable named
second_num
- line 4: Add
first_num
andsecond_num
and put the result in a third variable namedtotal
- line 6: Print out the string
"the sum is"
and the value of the variable namedtotal
Practice: Add three numbers
Solution: Add Three Numbers
first_num = int(input("enter first number: "))
second_num = int(input("enter second number: "))
third_num = int(input("enter third number: "))
# change the line below
total = first_num + second_num + third_num
# edit the text that will show up
print("the sum of the three numbers is: ", total)