PYTHON- Program that prints on the screen all numbers divisible by 7 but not multiples of 5, between 3000 and 3200 (inclusive)

1

Hello,
I am doing the following program:
The program should print all numbers divisible by 7, but not multiples of 5, between 3000 and 3200 (inclusive).

It turns out that I already did the code, but this one is not working in the best way and I can not solve it.

print("="*25)
print("Números divisíveis por 7")
print("="*25)
# modifique as 2 proximas variaveis para testar o programa
numeroDe = 3000;
numeroAte = 3200;

pares = 0;
impares = 0;
divPorSete = 0;

while (numeroDe <= numeroAte):
    if numeroDe % 2 == 0:
        pares += 1;
    else:
        impares += 1;

    if numeroDe % 7 == 0:
        divPorSete += 1;

    numeroDe += 1;

print ("Resumo de divisiveis entre ", numeroDe, " e ", numeroAte)
print ("\t- Números pares: ", pares)
print ("\t- Números impares: ", impares)
print ("\t- Números divisiveis por 7: ", divPorSete)

Try the online code

I ask for your help.

    
asked by anonymous 27.04.2018 / 10:34

3 answers

4

You are doing a lot of things (and I do not even understand why), you can use the module to see if the rest of the division of% by_% by 7 is 0, and if the remainder of the division of% by_% by 5 is different from 0.

You can simply do:

for i in range(3000, 3201): # percorrer todos os nums entre 3000 e 3200 (inclusive)
    if(i%7 == 0 and i%5 != 0): # modulo para ver se e multiplo de 7 e nao de 5
        print(i) # imprimir num

STATEMENT

If you want to keep the while loop:

num_currente = 3000;
numeroAte = 3200;
while num_currente <= numeroAte:
    if(num_currente%7 == 0 and num_currente%5 != 0): # modulo para ver se e multiplo de 7 e nao de 5
        print(num_currente) # imprimir num
    num_currente += 1

DEMONSTRATION

Otherwise, I noticed that you're wondering how many even / odd numbers you have in this range, you can calculate this without having to  add anything within the loop:

...
dif = numeroAte - numeroDe
pares = dif//2
impares = pares
if(dif%2 != 0):
    impares += 1

DEMOSTRATION

    
27.04.2018 / 11:01
1

What is the problem exactly? Why is your code not working the best?

I've changed your code a bit. First it was necessary to copy the value of the variable number, since it was printed at the end to say where it started. Since you were iterating the same variable you would use to print, the value at the end appeared different.

And I put the condition to be divisible by 7 but not to be a multiple of 5. Previously it only had the condition of being divisible by 7. What decreased the value from 29 to 23.

print("="*25)
print("Números divisíveis por 7")
print("="*25)
# modifique as 2 proximas variaveis para testar o programa
numeroDe = 3000;
copiaNumeroDe = 3000;

numeroAte = 3200;

pares = 0;
impares = 0;
divPorSete = 0;

while (numeroDe <= numeroAte):
    test = numeroDe

    if test % 2 == 0:
        pares += 1;
    else:
        impares += 1;

    if ((test % 7 == 0) and (test % 5 != 0)):
        divPorSete += 1;

    numeroDe += 1;

print ("Resumo de divisiveis entre ", copiaNumeroDe, " e ", numeroAte)
print ("\t- Números pares: ", pares)
print ("\t- Números impares: ", impares)
print ("\t- Números divisiveis por 7: ", divPorSete)

View the online code

    
27.04.2018 / 10:54
0
print([i for i in range(3003,3201,7) if i%5 != 0])
  • where 3003 is the first multiple of 7 in this range,
  • range(3003,32001,7) - runs 7 out of 7
29.04.2018 / 19:18