-2

My code is a PYTHON program that identifies if a number is prime or not. When I entered 45 however, it said that 45 was a prime, even though 45 isn't a prime number. Also, every time I run the program, it prints 'Sorry, the number you have entered is not prime.' or 'The number is indeed prime!' multiple times, instead of once. How do I make it print the output statements once and how can fix the program so that it says 45 IS NOT a prime number.

n = eval(input("Enter a number to find if that number is prime: "))
a = 2
while n > a:
    if ((n % a) == 0) & (a != n):
       print('Sorry, the number you have entered is not prime.')
    break
    else:
       print('The number you have entered is indeed prime!')
sarthak
  • 500
  • 9
  • 19
  • 1
    You shouldn't be using `eval` to parse integers, use `int` instead, while it's not really important in a learning example I'd suggest you [read Why is using `eval` a bad practice?](https://stackoverflow.com/questions/1832940/why-is-using-eval-a-bad-practice) for when it is important. – Nick is tired Jan 25 '18 at 21:43

2 Answers2

2

Because you are printing it every time. If you want to break after finding/not finding the prime number, indent one more level for the break.

Also, this does not calculate prime numbers. It calculates if its even number.

Follow the solution here

Sujil Maharjan
  • 1,307
  • 10
  • 12
1

Your code has some issues. To start with, you're never updating a, so you only ever check if the number n is even.

Once you fix that, you have two indentation problems. The first is that the break line needs to be inside the body of the if statement. Indent it more so that it's inside the if block.

The second indentation issue is more subtle. If you leave the else where it is, it will print out that the number is prime every time you test a potential factor that doesn't divide the number. That's both unhelpful (since it prints a lot) and wrong (since it says the number is prime even if it will later find a factor and say it's not prime). You can fix this by unindenting the else line so that it is lined up with the while statement. Using an else after a loop is an obscure bit of Python syntax. The body of the else only runs if the condition of the loop fails. It gets skipped if the loop exits due to a break statement.

Here's all of those necessary fixes together:

while n > a:
    if ((n % a) == 0) & (a != n):
        print('Sorry, the number you have entered is not prime.')
        break                                # indent this line more!
    a += 1                          # increment a, so you don't keep checking 2 over and over
else:                                        # unindent this line (and the next line too)
    print('The number you have entered is indeed prime!')

There are some other things that could be improved in your code, though they aren't causing it to run incorrectly. I'd recommend using int instead of eval to parse your number, and I'd use the logical-and operator and instead of the bitwise-and operator & in the if statement (though actually you don't need either, since the a != n check is redundant, as the loop would have already ended if it was true).

Blckknght
  • 100,903
  • 11
  • 120
  • 169
  • @NickA: The bitwise-and operator actually works OK for logical-and as long as its arguments are actually `bool` instances. That's because `bool` is a subtype of `int`, with `True` equal to `1` and `False` equal to `0`. So I didn't fix that in the updated version of the code, just mentioned it later. – Blckknght Jan 25 '18 at 21:54
  • @Blckknght Ah, so it's only important to avoid it if any parts of the condition aren't `bool`s? – Nick is tired Jan 25 '18 at 21:54
  • 1
    @NickA: Yes. The `and` operator will call `bool` on its first argument as necessary to asses its truth value. The `&` operator will not, since it expects to be working on numbers. `1 and 2` is truthy (it will be `2`, due to `and`'s short-circuiting behavior), but `1 & 2` is `0`, which is falsey. – Blckknght Jan 25 '18 at 21:57