Cycle problem in Python


I have this approach:


Read 10 whole numbers, store them in a list and determine   how many numbers of those stored in that list end in 15

I have this code, but I do not know where it is that I am wrong; For more than I look, I do not know where the error is.

He always tells me that there are no numbers ending in 15:

This is my code:



    for i in range(10):
        numero=int(input("Digite un numero entero: "))

    for l in range(len(lista)):
        while numeros>0:
            if digito==5:
                if digito==1:


    if aumento>0:
        print("Hay %d"% + " numeros que terminan en 15")

        print("No hay numeros que terminen en 15")  

except ValueError:
    print("El valor digitado debe ser numerico")    
asked by Andress115 06.10.2018 в 18:45

2 answers


Your logic is wrong.

Take for example the number 215, which ends in 15 and let's examine step by step what happens in your while loop.

The variable numeros (bad name, why plural if you only have one number?) would start with 215 . When doing digito=numeros%10 the result is 5 , so the if comes in. For the time being fine.

But within if you do digito=numeros//10 , which is the entire division between 10 and therefore produces 21 . In this case digito==1 is not met and the variable aumento is not increased (bad name, why not call it contador , since that is what it does?). Actually we did not want to look at 1 , but end in 1.

Then you make numeros = numeros//10 , so that numeros happens to be worth 21 and you repeat the loop. In this second iteration, it is no longer true that numeros%10 is 5 (it does not make sense to look at that, one would have to look in this case if it is 1 to detect if it ends in 15), so it does not enter the if Finally numeros = numeros//10 would leave the variable with the value 2 and in the last iteration it will not increase aumento .

You can try to imagine the execution step by step with other input data, but it is clear that it only works correctly if the input data is 15 , and not if ends at 15.

Also, you make aumento=0 for each iteration of the loop for , so even if you put a%% data_of% that would be detected correctly and increase 15 , in the next iteration (next data) you would put it new to 0. Your program only gives the correct result if in the whole list no number ends in 15, or if only the last one is 15.


A much simpler way to detect if a number ends in aumento would be to calculate the rest of dividing by 100. If that remainder is 15, it ends in 15. That is, if 15 .

Apart from that, other minor corrections in your code:

  • The names of variables. Although it may seem like a detail, they are important. When you write code you do not do it just to tell the computer what to do (for that any variable name is valid), but also to communicate to other people (or yourself when you read it in a while) how the program works or in what way You were thinking when you wrote it. Programming is a way to communicate with other programmers.

    In this sense I would change the name numeros%100 == 15 and call it aumento , and change contador and call it numeros . It is also customary to call numero of the variables that act as a loop index (instead of the i that you have used).

    The variable l is not used. Can be removed. Anyway, it was not a good name either, what did you think to put in it? Would the numbers that met finish at 15? Then you could call it lista2 , or elegidos . The variable terminados_en_15 is also left over.

  • The way to go through a list in Python is usually acumulador , which causes the variable for numero in lista: to take the values of the elements in the list. The way you have done it: numero is the typical way of doing it in other languages (ex: C) that do not have a for i in range(len(lista)): numero = lista[i] loop as powerful as Python.

  • The for that would print how many numbers there are is wrong, it lacks the variable to print.

  • The print() block is too large. You are trying to capture the case in which the user enters something that can not be converted into a number. That can only happen during the reading of data, so I would reduce the block try/catch exclusively to that reading, leaving out the processing part of the list.

That is, I would leave it like this:


    for i in range(10):
        numero=int(input("Digite un numero entero: "))
except ValueError:
    print("El valor digitado debe ser numerico")

contador = 0
for numero in lista:
    if numero % 100 == 15:
        contador += 1

if contador>0:
    print("Hay %d numeros que terminan en 15" % contador)
    print("No hay numeros que terminen en 15")
answered by 06.10.2018 в 19:25

If the objective is to capture the numbers that end in "15", I think the simplest thing would be, instead of using numerical types, to do it by means of strings. For example:

lista1 = [1, 23, 45, 215, 33, 441, 115, 15, 345]
resultado = [numero for numero in lista1 if str(numero).endswith("15")]

print("Hay %d números acabados en 15" % len(resultado))

What would give as output:

Hay 3 números acabados en 15
[215, 115, 15]
answered by 06.10.2018 в 19:26