Home > Software engineering >  Making a program in Python that tells if a number is a prime number, or not a prime number
Making a program in Python that tells if a number is a prime number, or not a prime number

Time:12-12

Im new here, hopefully i can ask question that is okey to ask here, but i've been fighting last hours with Python, im trying to make a program as simple as possible to tell the user if the number they choose to write in is a prime number or not a prime number. Thing is, i managed to make it work, then somehow i did'nt save it so lost it.

My code so far

n = int(input("Enter a number: "))
    
for i in range(2, n):
    if (n%i == 0):
        break
        quit (str(n)   " is not a prime number")
print (n, "is a prime number")

This makes my program tell the user that whatever number that is choosen, it will say "is a prime number" Before i did'nt have the break function there, i added it and changed something else and it worked somehow.

Any idea?...

Im very new to programming, python is my second language. I expect that the program to tell user to enter a number from number 2 to 101 if a number is a prime number it will print in console or a not a prime number and print it.

My idea: The program starts by asking for an integer. Then a for loop starts with a range from the number 2, up to 100. Once inside the loop, do the same check as we did in the Scratch program, that is, check if the number n modulo i = 0 where i starts at two and count up to the number we entered (but not including the number we entered). If we get an answer here that gives 0 in the remainder, then it is not a prime number and this is printed on the screen with the text "is not a prime number" and at the same time ends the program. If no answer is given where we get 0 in the remainder, then it is a prime number and the number together with the text "is a prime number" is instead printed on the screen.

I wish i could just ask a friend, but lets give this forum a shot!

Let me know if this kind of question is not suitable in this forum! Take care Chris

CodePudding user response:

There are couple of issue with your code. your break statement supressing the not prime print statemnt. Your print prime number is excluded from any loops hence it is printed every time.

Code correction

n = int(input("Enter a number: "))
for i in range(2, int(n/2) 1):
    if (n % i) == 0:    
        print(n, "is not a prime number")
        break
else:
    print(n, "is a prime number")

More simpler

n = int(input("Enter a number: "))
result = all(n % i for i in range(2, n))

if result:
    print(n, 'prime num')
else:
    print(n, 'not a prime num')
    

CodePudding user response:

n = int(input("Enter a number: "))

for i in range(2, n):
    if (n % i == 0):
        exit(str(n)   " is not a prime number")

print(n, "is a prime number")

The problem with your code is that it will always say that a number is prime, even if it is not. The reason for this is that it will never reach quit() as it will always break out of the loop and print that it is prime, even if it has a divisor other than 1 or the number itself.

You can simply fix this by removing the break from your code as it is not needed. This way, the program will terminate with your custom message if it is not prime and exit the loop safely into your success-message if it doesn't find any non-trivial divisors.

It also seems like using quit() is not advisable in scripts. If you still want to exit the program prematurely without importing anything else, you can use exit(), something that is available in all environments.

Alternative without quit() or exit():

n = int(input("Enter a number: "))

for i in range(2, n):
    if (n % i == 0):
        print(n, "is not a prime number")
        break
else:
    print(n, "is a prime number")
  • Related