My Coding Quiz #34 👨💻🛠️🧩
Welcome to the new installment of my series of Coding Quizzes, in which you will be able to test your knowledge and skills about programming and software development in a simple and fun way. If you want to learn more about it visit my blog here on Hive and the first post where I introduced it.
Without further ado, here's the riddle...

By @eniolw
What's your choice?
Solution to the previous quiz: SyntaxError. Line 1 creates a list called data
with numeric elements. The notation .2
is perfectly valid, equivalent to 0.2
as is common in spoken English and in other contexts.
Line 2 creates a new list nums
using list comprehension. Here a condition .1 < d
is introduced which if you find slightly difficult to read, you can think of it as d > .1
which is equivalent. The list nums
will consist of [0.2, 0.3] which are the only values greater than 0.1
in data
.
Line 3 creates a variable called sum
and this is critical to understanding the quiz, as we will see. It then starts a for loop that iterates over data
and accumulates its values in sum
.
After that, line 6 tries to create a total
variable from the sum of the elements of nums
and here we have a problem. The name sum
is partially reserved in Python for a built-in function that sums the elements of an iterable. However, that does not prevent it from being overwritten by the programmer and that is precisely what happened here in line 3 by saying sum = 0
. The behavior of sum
as a function has since been lost, which is why the code will generate a type error (TypeError), since the sum
object is a float and is not callable as a function. Ĺa last line would not be executed.
As you can see, you must be careful when naming your variables. The name sum
is very intuitive, but you should not use it in Python for such a thing.
If you want to blog about computer science and programming content, I invite you to join Hive and participate in its communities, such as STEM-social, Develop Spanish, Programming & Dev and others.
Mi Quiz de Programación #34 👨💻🛠️🧩
Bienvenido a mi nueva serie de Quizzes de Programación, en la cual podrás poner a prueba tus conocimientos y habilidades sobre programación y desarrollo de software de una manera sencilla y divertida. Si quieres aprender más sobre ella visita mi blog aquí en Hive y el primer post donde la presenté.
Sin más preámbulos, he aquí el acertijo...

Por @eniolw
¿Cuál es tu elección?
Solución al quiz anterior: SyntaxError. La línea 1 crea una lista llamada data
con elementos numéricos. La notación .2
es perfectamente válida, equivalente a 0.2
como es común en el inglés hablado y en otros contextos.
La línea 2 crea una nueva lista nums
usando la comprensión de listas. Aquí se introduce una condición .1 < d
que, si le resulta un poco difícil de leer, puede considerarla como d > .1
, que es equivalente. La lista nums
estará formada por [0.2, 0.3], que son los únicos valores mayores que 0.1
en data
.
La línea 3 crea una variable llamada suma
y esto es fundamental para comprender el cuestionario, como veremos. Luego inicia un bucle for que itera sobre data
y acumula sus valores en sum
.
Después de eso, la línea 6 intenta crear una variable total
a partir de la suma de los elementos de nums
y aquí tenemos un problema. El nombre sum
está parcialmente reservado en Python para una función incorporada que suma los elementos de un iterable. Sin embargo, eso no evita que el programador lo sobrescriba y eso es precisamente lo que sucedió aquí en la línea 3 al decir sum = 0
. Desde entonces, el comportamiento de sum
como función se ha perdido, por lo que el código generará un error de tipo (TypeError), ya que sum
El objeto es un flotante y no se puede invocar como una función. Ĺuna última línea no se ejecutaría.
Como puedes ver, debes tener cuidado al nombrar tus variables. El nombre sum
es muy intuitivo, pero no deberías usarlo en Python para tal cosa.
Si quieres bloguear sobre contenido informático y de programación, te invito a unirte a Hive y participar en sus comunidades, tales como STEM-social, Develop Spanish, Programming & Dev y otras.