This is a really cool use of the for ... else construct, which Python provides (and most other programing languages do not).
The else will get active at the end of the for loop, if it wasn't prematurely ended through break. So do not listen to those saying, that the else should be indented to the level of the if! You're right, it belongs to the for. But your break is wrongly indented. You want to end the loop the moment you found a divisor. So it is supposed to be part of the if-body, just like the print statement above it. Another thing that goes wrong is the numbers that you check: 1 will always divide any whole number without a rest, even prime numbers. So you need to start at 2 (and end before n, but your range is doing that already, a range does not include the upper border). Fun fact: You do not even need to go up ton-1to check all possible divisors.n//2would be a smaller border and evensqrt(n)would still work and check even less numbers.
Another very important hint: Do not useeval()**!** This function will take a string and evaluate it, execute it. If the string is code, it will be executed. Someone who uses your program could - instead of entering a number - enter malicious code to delete files or look up passwords and it would be executed (given Python has the rights through the operating system). This is incredibly dangerous. Never execute data or inputs as code. Instead here you can just use the int() function, which will return the input as an integer.
All in all we would therefore have the following:
n = int(input("Enter your number: "))
for i in range(2,n//2):
if n%i == 0:
print(n, "is not prime.")
break
else:
print(n, "is a prime number!")
Two things you could explore next to make your program even cooler:
Pack your prime checking into a function isPrime(n), which you define yourself to be given an n and return True or False. Your program around it asks for the input, checks with the function and prints an answer.
Look up error handling with try: and except: to handle user inputs which are not numbers. Currently those make your program crash. You might even want to use a while-loop in order to ask again and again as long as the stupid user does not enter a number. ;)
Oh and I forgot one thing: 0 and 1 are not prime numbers, but in my code they would be seen as prime. Let's see if you can maybe figure that one out by yourself. ;)
1
u/thw31416 Aug 31 '25
Hey there!
Great work so far!
This is a really cool use of the for ... else construct, which Python provides (and most other programing languages do not).
The
elsewill get active at the end of theforloop, if it wasn't prematurely ended throughbreak.So do not listen to those saying, that the
elseshould be indented to the level of theif! You're right, it belongs to thefor.But your
breakis wrongly indented. You want to end the loop the moment you found a divisor. So it is supposed to be part of theif-body, just like theprintstatement above it.Another thing that goes wrong is the numbers that you check:
1 will always divide any whole number without a rest, even prime numbers. So you need to start at 2 (and end before n, but your range is doing that already, a range does not include the upper border).
Fun fact: You do not even need to go up to
n-1to check all possible divisors.n//2would be a smaller border and evensqrt(n)would still work and check even less numbers.Another very important hint:
Do not use
eval()**!**This function will take a string and evaluate it, execute it. If the string is code, it will be executed.
Someone who uses your program could - instead of entering a number - enter malicious code to delete files or look up passwords and it would be executed (given Python has the rights through the operating system). This is incredibly dangerous. Never execute data or inputs as code.
Instead here you can just use the int() function, which will return the input as an integer.
All in all we would therefore have the following:
Two things you could explore next to make your program even cooler:
Pack your prime checking into a function
isPrime(n), which you define yourself to be given an n and returnTrueorFalse. Your program around it asks for the input, checks with the function and prints an answer.Look up error handling with
try:andexcept:to handle user inputs which are not numbers. Currently those make your program crash. You might even want to use awhile-loop in order to ask again and again as long as the stupid user does not enter a number. ;)