wrong structure in my code! does not fall in any condition

Asked

Viewed 121 times

-1

I need to make a python script using WHILE and IF to find out if a typed number is prime or not.

Below follows my code. I can not find where this my mistake, because if I type any value above 2 does not fall in the conditions I treated.

I know it’s a rookie mistake, I think the error is in the placement of the lines inside the structures. In the code I’m treating the divisions of the number typed from 1 to himself. I am not knowing how to format my code here on the site so I will post it in text and image.

Numero = int(input("Digite um numero inteiro: "))
if Numero == 2:
        print("Seu numero 2 é primo")
ContadorDeDivisores = 0
Divisor = 1
cont = 0
if Numero > 2:
    while cont < Numero:
            if Numero % Divisor == 0:
                ContadorDeDivisores = ContadorDeDivisores + 1
                Divisor = Divisor + 1
                cont = cont + 1
    if ContadorDeDivisores > 2:
        print("Seu numero",Numero,"não é primo")
    else:
        print("Seu numero",Numero,"é primo")

while Numero == 0 or Numero == 1:
    print("O numero que você quer verificar se é primo tem que ser maior ou igual a 2")
    Numero = int(input("Digite novamente um numero inteiro: "))
    if Numero == 2:
        print("Seu numero 2 é primo")
    if Numero > 2:
        while cont < Numero:
            if Numero % Divisor == 0:
                ContadorDeDivisores = ContadorDeDivisores + 1
                Divisor = Divisor + 1
                cont = cont + 1
        if ContadorDeDivisores > 2:
            print("Seu numero",Numero,"não é primo")
        else:
            print("Seu numero",Numero,"é primo")

meu código

1 answer

0


There are many things that can be improved in your code. As a suggestion, you could check the user input right at the beginning, and only break and loop once, given that the input was adequate. That way, you don’t have to write that while twice, nor the prints.

Another suggestion is to initialize the "Divisor" with 2, so you wouldn’t need to check if the number of divisors is greater than two. If there is a divisor beyond 1 you already know that it is not prime, and can even stop the flow before.

Finally, I do not believe that you need a "Cont" variable. All the necessary counting is already done by the variable "Divisor".

All that aside, the error in your code is that the increment of "Divisor" and "Cont" should be done outside If. That is, with words, you should try the next divisor regardless of whether the current case was an integer divisor or not, as follows:

while Divisor < Numero:
            if Numero % Divisor == 0:
                ContadorDeDivisores = ContadorDeDivisores + 1
            Divisor = Divisor + 1

There is one last hint, more mathematical, that says you only need to test until Divisor is larger than the square root of the requested number, but I believe that this is not suitable for a program with didactic purpose. (Or prevent the user from typing negative numbers for example).

With this modification your program works correctly:

Digite um numero inteiro: 5
Seu numero 5 é primo 
Digite um numero inteiro: 6
Seu numero 6 não é primo

Browser other questions tagged

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