Problem with python cycle

0

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 which numbers you enter, it always prints what is in the else, and if I delete the two "else" the program works but it is incomplete because I also need you to tell me when the "if" is not fulfilled

    
asked by Andress115 25.08.2018 в 22:07
source

2 answers

1

The first thing you have wrong are the break sentences, that makes it leave the for. The second thing that is wrong is that you go through the loop twice, one from minor to major and another from major to minor. Here I put the code arranged:

try:
    numero1=int(input("Digite el primer numero entero: "))
    numero2=int(input("Digite el segundo numero entero: "))
    multiplos = list()

    for i in range(numero1+1,numero2):
        if i % 5 == 0:
            multiplos.append(i)
    if multiplos: #Si stá vacía es False
        print("\nLos números múltiplos de 5 entre "+str(numero1)+" y "+str(numero2)+": ")
        print(multiplos)
    else:
        print("No hay multiplos de 5 comprendidos entre ambos numeros")
except ValueError:
    print("El valor digitado debe ser numerico")    

To prevent the message from being displayed on the screen: There are no multiples of 5 included between the two numbers I have created a list that will make the output cleaner.

Also, when you generate a range(from,to) by default the numbers are 1 in 1, it is not necessary to indicate that it goes 1 in 1 as you had it, unless you want to generate them in another order.

Greetings!

    
answered by 26.08.2018 в 12:49
0

it is not necessary to use the for cycle, since the number starts to go through the function, for example, if you enter 5 and 3, it will tour 8 times and show you 7 times that it is not a multiple of 5 and 1 time yes. 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")     
    
answered by 25.08.2018 в 22:18