When you use a for loop, you are iterating the number of times that are in range(1, int(user_input)) and so your if/else gets executed over and over again, and in each iteration is printing something. Instead you only want to print when you are done.

To pull this off, you want to use a boolean True/False variable to track if the number is prime or not, and set it accordingly in your for loop.

user_input = input("pick a number")

is_prime = True
for i in range(2, int(user_input)):
    if int(user_input) % i == 0 :
        is_prime = False
        break #break out of the for loop since the number isn't prime

if is_prime:
   print("Your number is prime")
else:
   print("your number is not prime")

Some other small changes:

  1. Removed condition and i != 1 as any iteration past 1 would fail this test and you want to check EVERY number in your iterations
  2. Removed condition and i != int(user_input) since in your iterations you will never reach this number range() is not inclusive of the highest number.
  3. Added a break once we determine that the number is not a prime so we don't waste iterations checking if it's even more not-prime.
Answer from JNevill on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-program-to-check-whether-a-number-is-prime-or-not
Check Prime Number in Python - GeeksforGeeks
In the sympy module, we can test whether a given number 'n' is prime or not using sympy.isprime() function. For n < 264 the answer is definitive; larger n values have a small probability of actually being pseudoprimes.
Published ย  October 11, 2025
Top answer
1 of 4
4

When you use a for loop, you are iterating the number of times that are in range(1, int(user_input)) and so your if/else gets executed over and over again, and in each iteration is printing something. Instead you only want to print when you are done.

To pull this off, you want to use a boolean True/False variable to track if the number is prime or not, and set it accordingly in your for loop.

user_input = input("pick a number")

is_prime = True
for i in range(2, int(user_input)):
    if int(user_input) % i == 0 :
        is_prime = False
        break #break out of the for loop since the number isn't prime

if is_prime:
   print("Your number is prime")
else:
   print("your number is not prime")

Some other small changes:

  1. Removed condition and i != 1 as any iteration past 1 would fail this test and you want to check EVERY number in your iterations
  2. Removed condition and i != int(user_input) since in your iterations you will never reach this number range() is not inclusive of the highest number.
  3. Added a break once we determine that the number is not a prime so we don't waste iterations checking if it's even more not-prime.
2 of 4
0

There are some issues with your code range should be 2 not 1. Add a break statemnt once condition satisfies.

user_input = input("pick a number")
user_input  = int(user_input)

for i in range(2, user_input)::  #for i in range(2, int(user_input /2)+1) also works
    if (user_input  % i) == 0:    
        print("Your number is prime")
        break
else:
    print("Your number is not a prime")

More simpler approch

user_input = int(input("pick a number"))
result = all(user_input % i for i in range(2, user_input))

if result:
     print("Your number is prime")
else:
     print("Your number not a is prime")
Discussions

Prime number or not?
One trick you can use is that you dont have to go over all numbers from 2 to N, you only need to check numbers from 2 to int(sqrt(N)) + 1. Reason is math, lets say N is 32, it doesnt matter if you check 4 * 8 or 8 * 4, you can kinda omit the second one as if 32 is divisible by 4 it is also divisible by 8 and you dont even need to check it. Essentially you dont need to check the second reversed pair. Question is, from what number you do not have to check, the answer is that you dont have to check from the point where the first number is better then the second one, because of reasons above. This point happens when X * X = N, so you only need to check from 2 to sqrt(N). More on reddit.com
๐ŸŒ r/learnpython
27
54
December 3, 2022
This is a question about a program to determine whether a number is a prime number
Could the following code be used to determine whether a number is prime? Iโ€™ve tested it and canโ€™t find any exceptions. Could the following code be used to determine a prime number? def prime_checker(number): if numbโ€ฆ More on discuss.python.org
๐ŸŒ discuss.python.org
0
May 4, 2023
Prime number finding function
A function can only return once, so you're returning False as soon as you discover the number is even. More on reddit.com
๐ŸŒ r/learnpython
7
0
January 21, 2021
Finding Prime Numbers
Two easy ways to add elements to a list: The first method is to create an empty list, then append new elements to it. ceiling = int(input('Highest number to check? ')) primes = [] for number in range(1, ceiling + 1): if is_prime(number): primes.append(number) The second method is to use list comprehension. ceiling = int(input('Highest number to check? ')) primes = [number for number in range(1, ceiling + 1) if is_prime(number)] Your function to check if a number is prime can be set up in a number of ways. Here's one: def is_prime(number): if number < 2: prime = False else: hi_number = int(number ** 0.5 + 1) prime = all(number % i for i in range(2, hi_number)) return prime Good luck! Edit: oops in my prime check More on reddit.com
๐ŸŒ r/learnpython
14
9
July 9, 2019
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ examples โ€บ prime-number
Python Program to Check Prime Number
You can change the value of variable num in the above source code to check whether a number is prime or not for other integers. In Python, we can also use the for...else statement to do this task without using an additional flag variable. num = 407 # To take input from the user #num = int(input("Enter a number: ")) if num == 0 or num == 1: print(num, "is not a prime number") elif num > 1: # check for factors for i in range(2,num): if (num % i) == 0: print(num,"is not a prime number") print(i,"times",num//i,"is",num) break else: print(num,"is a prime number") # if input number is less than # or equal to 1, it is not prime else: print(num,"is not a prime number")
๐ŸŒ
NxtWave
ccbp.in โ€บ blog โ€บ articles โ€บ python-program-to-check-prime-number
Python Program to Check Prime Number With Examples
1 month ago - In the example, the function will check if the number 29 is prime and print is_prime is True, which means the number 29 is prime. The isprime function in Python is used to check whether the number is prime or not.
๐ŸŒ
Quora
quora.com โ€บ How-can-a-beginner-write-a-code-in-Python-to-check-if-a-number-is-prime
How can a beginner write a code in Python to check if a number is prime? - Quora
Answer (1 of 7): Letโ€™s think this through. What makes a positive integer prime? Well, there is one thing for sure. For example, take the prime number 7. What youโ€™ll notice is that the numbers below them, from 2 to 6, cannot divide 7. Same applies to every prime number.
๐ŸŒ
Great Learning
mygreatlearning.com โ€บ blog โ€บ it/software development โ€บ prime numbers program in python
Prime Numbers Program in Python | How to check prime numbers
January 6, 2025 - To find prime numbers within a range in Python, follow these steps: Input the lower and upper range from the user. Loop through numbers within the range. For each number, check if it's greater than 1.
Find elsewhere
๐ŸŒ
Geekflare
geekflare.com โ€บ development โ€บ how to check if a number is prime in python
How to Check if a Number is Prime in Python
December 28, 2024 - This tutorial will teach you how to write a Python program to check if a number is prime or not, both O(n) and O(โˆšn) algorithms.
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
This is a question about a program to determine whether a number is a prime number - Python Help - Discussions on Python.org
May 4, 2023 - Could the following code be used to determine a prime number? def prime_checker(number): if number == 1: print("It's not a prime number.") if number % 2 == 0 or number % 3 == 0: print("It's not a prime number.") else: print("It's a prime number") ```
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 3236822 โ€บ python-code-to-find-given-number-is-prime-or-not
Python code to find given number is prime or not? | Sololearn: Learn to code for FREE!
Here You Can Have the Code def ... number = int(input("Enter a number: ")) if check_prime(number): print(number, "is a prime number.") else: print(number, "is not a prime number.")...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ python-program-to-check-if-a-number-is-prime-or-not
Python program to check if a number is Prime or not
August 22, 2023 - # Number to be checked for prime n = 5 # Check if the number is greater than 1 if n > 1: for i in range(2, int(n/2)+1): if (n % i) == 0: print(num, "is not a prime number") break else: print(n, "is a prime number") # If the number is less than 1, its also not a prime number.
๐ŸŒ
Programminginpython
programminginpython.com โ€บ python-program-find-number-prime-composite
Python program to find a number is prime or composite
March 29, 2025 - num = int(input("Enter any number : ")) if num > 1: for i in range(2, num): if (num % i) == 0: print(num, "is NOT a prime number") break else: print(num, "is a PRIME number") elif num == 0 or 1: print(num, "is a neither prime NOR composite number") ...
๐ŸŒ
STEMpedia
ai.thestempedia.com โ€บ home โ€บ examples โ€บ check if a number is prime or not | pictoblox
Check If a Number is Prime or Not | Pictoblox - Example Project
August 1, 2023 - In this Python program, we take a number as input from the user and check whether it is a prime number or not. We assume that the number is a prime number by default and then iterate from 2 to half of the number.
๐ŸŒ
ScholarHat
scholarhat.com โ€บ home
Python Program to Check Prime Number
September 10, 2025 - To print 1 to 100 prime numbers in Python, use a function that checks and prints all prime numbers between 1 to 100 such as is_prime(). To print 10 prime numbers in Python, you can use a loop and the 'is_prime_while' function.
๐ŸŒ
DaniWeb
daniweb.com โ€บ programming โ€บ software-development โ€บ code โ€บ 216880 โ€บ check-if-a-number-is-a-prime-number-python
Check if a number is a prime number (Python) | DaniWeb
This simple isprime(number) function checks if the given integer number is a prime number and returns True or False. The function makes sure that the number is a positive integer, and that 1 is not considered a prime number.
๐ŸŒ
Rookie's Lab
rookieslab.com โ€บ posts โ€บ fastest-way-to-check-if-a-number-is-prime-or-not
Fastest way to check if a number is prime or not - Python and C++ Code | Rookie's Lab
January 21, 2017 - So, if we find any factor in this range, then we call that number a non-prime number, else it is a prime number. Time Complexity of the above algorithm is O(sqrt(N)). Go ahead, try to check for a number as large as 1000000000, this implementation will return the result in flash.
๐ŸŒ
Upgrad
upgrad.com โ€บ home โ€บ tutorials โ€บ software & tech โ€บ prime number program in python
Prime Number in Python โ€“ 7 Easy Tricks That Work for You
August 25, 2025 - To determine if a number is prime in Python, you must check whether it is divisible only by 1 and itself. A basic method involves iterating through potential divisors up to the square root of the number.
๐ŸŒ
WsCube Tech
wscubetech.com โ€บ resources โ€บ python โ€บ programs โ€บ prime-number
Prime Number Program in Python (7 Ways With Code Logic)
October 28, 2025 - Python program to check prime numbers in 7 different ways with code examples. Learn various methods to determine if a number is prime efficiently.
๐ŸŒ
Vultr
docs.vultr.com โ€บ python โ€บ examples โ€บ check-prime-number
Python Program to Check Prime Number | Vultr Docs
December 9, 2024 - It then loops through numbers from 2 up to the integer value of the square root of the number, checking for divisibility. If any number divides the input number evenly, it is not prime.
๐ŸŒ
LeetCode
leetcode.com โ€บ problems โ€บ count-primes
Count Primes - LeetCode
Count Primes - Given an integer n, return the number of prime numbers that are strictly less than n. Example 1: Input: n = 10 Output: 4 Explanation: There are 4 prime numbers less than 10, they are 2, 3, 5, 7. Example 2: Input: n = 0 Output: ...