Output and Input
Most Python programs start off at a terminal or text console. To begin with, our programs will be able to output text, and receive text as input.
Later on you can learn to create graphical interfaces, but that's a bit more advanced than what we'll look at in this course!
To show something to the user in text format, we use Python's
A function in Python is something that performs an action, as opposed to a variable which is a name for a value.
Let's try to print something in your editor (or use the editor below):
You can press the green "Play" button to execute the Python code, right in your browser!
As you progress through this chapter, you can edit the code that you see above to try out different code snippets (although if you have a repl.it account, creating separate repls for different concepts is an excellent idea).
Printing variables is straightforward! Instead of putting a string inside the
print() brackets, just put your variable.
age = 30 print(age)
Go on, try that code out in the editor above. Remember to always type the code. Don't copy and paste it, as that will not help you learn as quickly!
Make sure to not surround the variable with quotation marks, otherwise that makes it a string. Don't do
Getting user input
We can ask the user to give us data so that we can use it in our programs. We do that with the
print(), we also put something inside the brackets of
input(). Try this out in your editor or the editor above!
age = input("Enter your age: ") print("Your age is: " + age)
Maths with user input
All the user input that we get via the
input() function is a string. That means we can't do maths on it, unless we convert it to a number first.
Converting a string into a number is simple, but there can be problems. Here's how to convert a string into an integer:
age = input("Enter your age: ") age_as_number = int(age) print("Your age in months is below.") print(age_as_number * 12)
By using the
int() function, we put the string we want to convert inside the brackets, and that will create a number with value equal to the string contents.
Remember we can't add strings and numbers together, so that's why we have two
print() in there!
First problem: is it a number?
The first problem we can encounter when trying to do
int(age) is that
age may not be something we can convert.
age = "Rolf", then
int(age) will fail. That's because
"Rolf" is not something that can be converted into an integer.
Indeed if we do this:
We will get an error like so:
Traceback (most recent call last): File "main.py", line 1, in <module> int("Rolf") ValueError: invalid literal for int() with base 10: 'Rolf'
A helpful, yet cryptic error. It's in four lines:
- The first one tells us that we're about to read a Traceback. That means where the error happened.
- The second line tells us where the error happened, in a file called
main.py, line 1.
- The third line tells us the actual line of code that caused this error.
- The last line gives us an oft-unhelpful description of the error. Here the description is not too bad, says that "Rolf" is an invalid literal (string) for the
Second problem: adding them together
Adding a string and a number together is not something we can do, so in our code above we worked around it.
However, just as you can turn a string into an integer, you can go the other way.
age = input("Enter your age: ") age_as_number = int(age) months = age_as_number * 12 months_as_string = str(months) print("Your age in months is: " + months_as_string)
By using the
str() function, we put a number inside the brackets and Python creates a string. We then give that string the name
months_as_string, and we can use it in our program.
Remember to try it all out in the editor!