My Coding Quiz #34

Words
736
Reading
4 min
3y
Listen to this post
~5 min

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...




Quiz
By eniolw@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...




Quiz
Por eniolw@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.

My Coding Quiz #34 | Ecency