13
u/echols021 Aug 27 '25
Let's help you learn how to solve your own problems. You've got two main options:
put in more
printstatements to see different variables' values at different points, and get a better idea what it's actually doinguse your IDE to put on breakpoints and run it in debug mode, watching it run line by line, including all the variables' values
5
u/jamuzu5 Aug 27 '25
For the first loop, i =1.
First loop if statement:
Is n%1 == 0? Yes!
Next line prints "it is not a prime number"
Break and exit the loop.
Also, your last 3 lines aren't indented enough. Needs 1 more tab on each line.
4
u/calculus_is_fun Aug 27 '25
Just the break needs to be indented, the else block belongs to the for loop
1
u/thw31416 Aug 31 '25
Exactly. The else belonging to the if would be wrong, as a number is only not a prime when all numbers have been checked. So yes, this is best as a for...else in python with the else getting active whenever the loop didn't break prematurely. Quite a neat construct.
3
u/timheiko Aug 27 '25
Here are my 2c:
start the loop with 2, instead of 1 because n % 1 is always 0.
indent the break inside the if-statement, i.e. four spaces to the right, so that the loop breaks only if the if’s condition evaluates to True.
[a nitpick] use int(…) instead of eval(…) since eval(…) does more than you probably need in the program.
Enjoy your coding journey!
1
u/thw31416 Aug 31 '25
The eval is pretty scary there, executing user input as code. Someone putting in "import os; os.remove(...)" might be able to do quite some damage...
1
u/Drakhe_Dragonfly Aug 27 '25
I'm not entirely sure, but I think that X mod 1 is always 0, that's why it doesn't gives you the correct result.
Also, instead of n for the upperbound up to which you check, you can take n/2 (technically you could go down to the sqareroot of n without any problems, but that's optional)
1
u/Darkrider1325 Aug 27 '25
I am not a pro in python so I just wanted to ask. Is that break needed?
2
u/JAVA_JK Aug 27 '25
I also think so that break is not required, try removing it and see if that works. Should work
2
Aug 27 '25
The break is needed, but with one more indentation: you break the loop as soon as you have found a proper divisor (so not 1, that's another bug). It's not only faster, it's mandatory here because he's using a for loop with an else clause, a somewhat dirty feature of Python I never used in 24 years of Python. As the documentation [*] explains, the else clause isn't executed if a break occurred.
Of course there are other optimizations, even with the naive trial division algorithm: only check 2 and odd numbers > 2 and <= sqrt(n).
[*] https://docs.python.org/3/reference/compound_stmts.html#for
1
1
1
1
u/FanUpstairs8699 Aug 27 '25
i think this works
n=int(input("enter a number"))
is_prime = True
for i in range(2,n):
if n%i==0:
is_prime = False
break
if n == 1 or n == 2:
print("neither")
elif is_prime:
print("Prime")
else:
print("Not prime")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 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 to n-1 to check all possible divisors. n//2 would be a smaller border and even sqrt(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:
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 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. ;)
1
u/thw31416 Aug 31 '25
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/80845 Aug 31 '25
why dont people use chatgpt to ask these questions? Seems like creating random problem then creating new post harversting those precious internet points where people jump over each other showing their programming capabilities solving said problem?
18
u/DubSolid Aug 27 '25
Win + PrtScn would be a great place to start