In this post from programming course in Python using Linux We will continue talking about how to document what the code does. This is a very useful exercise since our memory isn't always reliable when it comes to recalling what we write. We'll also introduce the topic of loops.
On the other hand, if we write code professionally or are part of an open-source project community, it's likely that other people will need to make modifications, so they need to know where they stand. Loops allow us to keep executing programs until a condition is met.
In the previous article we introduced the concept of docstrings. Docstrings allow us to access brief explanations about what certain functions, classes, methods, and modules do. without needing to stop the program's execution and read all the code until you find a comment. But before we continue, let's review some concepts.
- Function: Reusable block of code that performs a specific task.
- Class: It is a mold for defining the objects we create from it.
- Method: It is a function that belongs to a class.
- Module: It is a Python program that can be executed within another Python program.
Class and function are things we explained in previous articles. We'll talk about modules later.
Docstrings
Docstrings are text strings qThese are placed at the beginning of a function and contain a brief description of what the function does, what parameters it receives, and what values ​​it returns.
As we can see, enclosed in three double quotes is the text that explains how the function works and provides an example. Below is the code for the comparison.
The reasons for using docstrings are:
- Clarity: They help the person who has to review the code understand more quickly what each thing does.
- Ease of access: The documentation can be accessed from the interactive console.
- Standards Compliance: The use of docstrings to document code is a convention widely accepted by the Python community.
- Update: PIt allows you to easily document changes in the code without having to generate new files.
At the beginning of this course, we recommend using Visual Studio Code as your integrated development environment and installing some additional extensions. VS Code automatically displays the text from the docstrings when you hover the mouse pointer over it. Other ways to do this include:
- From the interactive console: (The Linux terminal from which you run the Python application.
- From the same program code that we are running.
- From another program that imports the function.
These are examples with the command help()
First of all, a clarification. Due to an incompatibility between Python's indentation requirements and the blog's content management system, I can't directly paste very long code snippets. Pastebin, where I've been pasting the code in previous articles, has a limit on free usage, which I've already exceeded. That's why I'm using screenshots. In any case, copying the code by hand will help you understand how it works.
We can call docstrings directly from the terminal where we are running the program we are writing with the following commands:
from detectar_distro import obtener_info
help(obtener_info)
We should have previously saved the previous program as detect_distro.py
From the code of another program
# archivo: consulta.py
from detectar_distro import obtener_info
help(obtener_info)
Performing repetitive tasks
So far we've only seen programs that execute a task and then stop. But, In the real world, applications run continuously until the user stops them. One way to achieve this is by using loops; loops execute code as long as a certain condition is met.
The condition is set with the command while
This program creates the distribution variable and sets with the while command that until the user enters Ubuntu He is still being asked to write the name of a distribution.
While loops are very useful when we cannot specify how many times the code should be executed to obtain the desired result. For example, if we want to simulate a single roll of the dice and the result doesn't matter, we wouldn't need a while loop.
Some useful use cases are:
- Ask the user to enter data until the data entered is correct.
- Perform calculations until the expected result is achieved. (For example, when in school you knew the solution to a problem but not how to get there)
- Process the data in a list until you reach the end.
One way to use the while loop is by setting a counter with a number of attempts.
The program works as follows:
- A variable is created for attempts and set to 0, another sets the number of attempts to a maximum of 5, and a variable is created to store the user's input.
- The while loop is assigned two conditions: that the user has not guessed correctly and that they have not exhausted the number of attempts.
- Each time the user makes an attempt, the counter is incremented.
- The user is shown the attempt number.
- When either of the two conditions is met, it is determined whether the user won or lost.
Break and continue
There are two rulings that affect the functioning of a loop in addition to the fulfillment or non-fulfillment of the condition:
- Break: Break: It stops the loop's execution even if the conditions haven't been met and jumps to the first line of code outside the loop. This can be useful, for example, if the user wants to exit the program.
- Continue: It doesn't stop the entire loop; instead, it skips the current attempt and moves on to the next one. For example, in our program that asks for Linux distributions, if the user were to type "Windows," it could ask them to enter the name of a distribution again without performing the check.
In the next article we will continue to develop this topic.