3.1 Console
Back in the day, when computers were huge, you could connect to them over the phone network from a remote terminal. These terminals were called consoles. Since then, the terms
console output
and
console input
have come about.
Console output means displaying data (text) on the screen.
Console input means inputting data via the keyboard.
The console is a text-based user interface that allows you to interact with a computer by entering commands with the keyboard. In programming, the console is used to input and output data while running a program.
Practical use:
Console interaction is often used in scripts for automating tasks, in educational programs where user interaction is required, or as a simple interface for testing and debugging code. Understanding how to organize console input and output is an important programming skill.
Console applications are usually easier to develop and debug compared to graphical user interface applications. This makes them an ideal choice for many software tasks, especially those related to rapid prototyping or system task automation.
Console Output:
Console output is the process of displaying information for the user via the console. In Python, the function print()
is used for outputting data such as text, numbers, and other objects.
print("This message will be displayed on the console.")
Yep, you already know how to output data to the console. But for getting input from the console, we need to learn the function input()
.
3.2 The input() Function
In Python, to read data entered by a user through the console, the function input()
is used. It's one of the basic ways to interact with the user in console applications.
The entered data is always interpreted as a string (type
str
). So, if you're going to work with numbers or other data types, you'll need to convert the input string accordingly.
Using the function input()
is super straightforward. The function call can contain a string that will be shown on the screen before the user inputs data. This string serves as a prompt or guide indicating what data needs to be entered. For example:
name = input("Enter your name: ")
print("Hello ", name)
In this example, the user will be prompted to enter their name, and after entering it, the program will greet the user using the entered data.
You can use the function input()
without
text too, in which case it will just wait for the user to enter data on the
keyboard and hit enter
. Example:
name = input() # waiting for text input and enter
print("Hello ", name)
3.3 Entering Numbers from the Console
To enter numbers from the console, you need to convert the string to the appropriate numeric type, such as int
or float
, using the functions int()
or float()
. This is crucial for performing mathematical operations:
age = input("Enter your age: ") # contains a string
age = int(age) # convert the entered age to an integer
print("In 10 years you will be " + str(age + 10) + " years old.")
Sometimes data input and conversion to a number are written in a single line:
age = int(input("Enter your age: ")) # contains a number
print("In 10 years you will be " + str(age + 10) + " years old.")
Entering floating-point numbers is similar to integer numbers, but you need to use the float()
function:
age = float(input("Enter your age: ")) # contains a number
print("In 10 years you will be " + str(age + 10) + " years old.")
Using input()
requires attention in handling input, as incorrect input can lead to errors. For example, trying to convert a string to an integer that cannot be interpreted as a number will throw a ValueError
.
We'll learn what to do with such errors a bit later.
In conclusion, input()
is a versatile tool for reading user data in Python. It lets you easily and effortlessly gather inputted information but demands careful application, especially when type conversion and potential input errors need handling.
GO TO FULL VERSION