It would be nice if IT professionals could give consultations like doctors or lawyers, but something prevents us from charging for everything and this desire to share ideas consumes us.

I participate in several Telegram groups, mainly about Python, one of which is the PyCoding group and the other is the pybr. Normally I read the groups when I’m using my cell phone, so it’s not always possible to help with questions, but I’ll try to set aside a little time to explore some ideas here and there.

Today it’s very easy to learn anything that I’ve noticed an anxiety among those who start programming wanting to learn everything. In just one month, some people want to learn Python, SciPy, TensorFlow, Android, and whatever else comes up. A month is too short a time. You can learn to program in relatively small periods, but it takes time to get used to new ideas, languages, and libraries. Peter Norvig commented on this anxiety in Learn Programming in Ten Years.

Calculating averages#

Let’s go back to the interest of the post.

The colleague Wesley sent two programs, I’ll start with the simplest one. First, let’s ignore the profanity, our colleague is young.

One thing that pleased me was the first line of messages. Few people worry about saying what the program does, which is great! I would make only a small modification to avoid making the line too long.

As I grew up in the 80s without lowercase letters and accents, it’s a matter of honor to correct the messages.

In lines 7-11, the values of variables m1, n1, n2, n3, and n4 are requested. Since the input function returns strings, see that in the rest of the program the float function was used to convert these values. In this case, the converted value should be stored directly in the variable.

In this way, we simplify line 12 to easily perceive an error of priority of operations. When calculating n1 + n2 + n3 + n4 / 4 without using parentheses, the operations are performed by order of priority, as in mathematics. Thus, n4/4 is added to n1, n2, and n3. To calculate the average, we need parentheses: (n1 + n2 + n3 + n4) / 4. Now, the sum of notes is calculated and then divided by four, as we wanted.

Between lines 15 and 19 I think it was just a test. I’ll remove it to avoid disturbing the understanding of the final program.

Lines 24-35 print various points; I’ll simplify them.

To finish, small modifications to use f strings from Python 3.6.

Program with tkinter#

The other program is a graphical interface using tkinter.

As the sources were posted on Telegram, much was lost. Right away there’s a problem with the import of line 2. I congratulate Wesley for his courage in using tkinter. It’s one of the parts of Python that least like, but it works.

Avoid using import * in Python; it pollutes the namespace and causes problems. In the case of tkinter, it’s a special case to think about, but never mix the * with individual imports of classes and functions.

One thing that stands out is not the deep caladryl foundation, but the repetition of colors in various parts of the code. Let’s create a constant for the background color; better, let’s remove the colors and leave the default colors.

Another problem is the validation of values; I added a float_or_zero function that returns zero if the value entered cannot be converted to float.

Using tkinter without classes is somewhat confusing; I personally don’t like having global variables in functions and mixing function definitions and variable declarations, but that’s another post topic.

Let’s see how it turned out!

Converting ints#

Another interesting post was about converting multiple ints at once. The initial problem was to calculate a value of the type hh:mm:ss in total seconds.

One solution that caught my attention:

Correct, but I think the focus of the solution was not on the initial problem, but to do it in fewer lines. Suddenly, the fear of “Perlizing” Python comes up.

The problem itself requires data validation; this is an important detail that’s easy to forget. Instead of doing it in fewer lines, let’s add the minimum validation.

This solution uses the datetime module from Python and the time type for validating hours between 0 and 23, minutes between 0 and 60, and seconds. If the user enters an incorrect value, they will have to re-enter it after receiving an error message. Although I used list expansion twice in one line (Perlization?), I think the code turned out relatively well.

There’s still room for another solution, where we create a function to convert hours, minutes, and seconds to total seconds.

In addition to validation (albeit minimal), we gain flexibility in entering values like 10, 10:20, or 10:20:30. The program we made can be imported by other programs and its functions reused without losing the initial functionality if used as a main program.