0 votes

Problem with loop in python

Good afternoon, could you help me with this code please:

 try:

numero1=int(input("Digite el primer numero entero:"))

numero2=int(input("Digite el segundo numero entero:"))


    for i in range(numero1+1,numero2,1):
        if i % 5 == 0:
            print(i)

        else:
            print("No hay multiplos de 5 comprendidos entre ambos numeros") 
            break       

    for l in range(numero2+1,numero1,1):
        if l % 5 == 0:
            print(l)

        else:
            print("No hay multiplos de 5 comprendidos entre ambos numeros")     
            break

except ValueError:
    print("El valor digitado debe ser numerico")    

No matter what numbers I enter, it always prints what is in the else, and if I delete the two "else" the program does work but it remains incomplete because I also need you to tell me when the "if" is not fulfilled

0voto

Matthew Seidel Points 39

It is not necessary to use the for loop, since the number starts to loop through the function, for example, if you enter 5 and 3, it will loop 8 times and show you 7 times that it is not a multiple of 5 and 1 time that it is. the solution is:

 try:

  numero1=int(input("Digite el primer numero entero:"))
    if i % 5 == 0:
        print(i)

    else:
        print("No hay multiplos de 5 comprendidos entre ambos numeros") 

    if l % 5 == 0:
        print(l)

    else:
        print("No hay multiplos de 5 comprendidos entre ambos numeros")     

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X