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:
- Removed condition
and i != 1as any iteration past1would fail this test and you want to check EVERY number in your iterations - Removed condition
and i != int(user_input)since in your iterations you will never reach this numberrange()is not inclusive of the highest number. - Added a
breakonce we determine that the number is not a prime so we don't waste iterations checking if it's even more not-prime.
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:
- Removed condition
and i != 1as any iteration past1would fail this test and you want to check EVERY number in your iterations - Removed condition
and i != int(user_input)since in your iterations you will never reach this numberrange()is not inclusive of the highest number. - Added a
breakonce we determine that the number is not a prime so we don't waste iterations checking if it's even more not-prime.
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")
Prime number or not?
This is a question about a program to determine whether a number is a prime number
Prime number finding function
Finding Prime Numbers
Videos
I wrote the program, which shows, is number prime or not.
https://pastebin.com/e9dDqjYM
It works great in Visual Studio Code, but OnlineTutor returned me TLE (Time-limit exceeded). Any idea, how to rewrite code for effective solution?