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

Asked

Viewed 2,068 times

1

Hello,
I’m doing the following program:
The programme shall print on the screen all numbers which are divisible by 7 but are not multiples of 5 between 3000 and 3200 (inclusive).

Turns out I’ve done the code, but this one’s not working the right way, and I’m having trouble solving 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 code online

I ask for your help.

  • Have you tried anything? Put in that code that you have pff

3 answers

4


It’s doing a lot of unnecessary things (and I don’t really understand why), you can use the module to see if the rest of the i by 7 is 0, and if the rest of the division of i by 5 is different from 0.

You can just 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

DEMONSTRATION

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

Other than that, I noticed that you’re also wondering how many even/odd numbers you have in that range, you can calculate that without having to add nothing within the cycle:

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

DEMONSTRATION

  • It was the range cycle I wanted!! And I didn’t remember! Thank you very much!

  • @Joaopeixotofernandes, good, note that with the while gives in it. Good to find a compatriot here. Good luck

  • 1

    I know it does, but I really wanted it with the range, and I couldn’t do it.

1

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

I changed your code a little. First, I had to copy the value of the variable number, since it was printed at the end to tell where it started. As you were iterating the same variable you would use to print, the value at the end looked different.

And I put the condition of being divisible by 7 but not multiple by 5. Before 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)

See the code online

0

print([i for i in range(3003,3201,7) if i%5 != 0])
  • where 3003 is the first multiple of 7 in that range,
  • range(3003,32001,7) - runs every 7

Browser other questions tagged

You are not signed in. Login or sign up in order to post.