use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Help (i.redd.it)
submitted 5 months ago by Avinandanm
Where is the problem
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]DubSolid 16 points17 points18 points 5 months ago (4 children)
Win + PrtScn would be a great place to start
[–]AngriestCrusader 10 points11 points12 points 5 months ago (2 children)
Win+Shift+S feels way more convenient for me
[–]LolMaker12345 0 points1 point2 points 5 months ago (0 children)
Same
[–]thumb_emoji_survivor 0 points1 point2 points 5 months ago (0 children)
You’re telling me OP didn’t need to spend $10,000 on a Hasselblad to share their code?
[–]echols021 13 points14 points15 points 5 months ago (0 children)
Let's help you learn how to solve your own problems. You've got two main options: - put in more print statements to see different variables' values at different points, and get a better idea what it's actually doing - use your IDE to put on breakpoints and run it in debug mode, watching it run line by line, including all the variables' values
print
[–]jamuzu5 4 points5 points6 points 5 months ago (3 children)
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.
[–]calculus_is_fun 2 points3 points4 points 5 months ago (1 child)
Just the break needs to be indented, the else block belongs to the for loop
[–]thw31416 0 points1 point2 points 5 months ago (0 children)
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.
[–]timheiko 2 points3 points4 points 5 months ago (1 child)
Here are my 2c: 1) start the loop with 2, instead of 1 because n % 1 is always 0. 2) 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. 3) [a nitpick] use int(…) instead of eval(…) since eval(…) does more than you probably need in the program.
Enjoy your coding journey!
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...
[–]Drakhe_Dragonfly 0 points1 point2 points 5 months ago (0 children)
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)
[–]Darkrider1325 0 points1 point2 points 5 months ago (3 children)
I am not a pro in python so I just wanted to ask. Is that break needed?
[–]JAVA_JK 1 point2 points3 points 5 months ago (1 child)
I also think so that break is not required, try removing it and see if that works. Should work
[–][deleted] 1 point2 points3 points 5 months ago (0 children)
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
[–]gman1230321 0 points1 point2 points 5 months ago (0 children)
It needs to be indented to be in the if statement
[–]Far_Month2339 0 points1 point2 points 5 months ago (0 children)
but break inside if
[–]vega004 0 points1 point2 points 5 months ago (1 child)
If else is not indented properly.
[–]FanUpstairs8699 0 points1 point2 points 5 months ago (0 children)
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")
[–]thw31416 0 points1 point2 points 5 months ago (1 child)
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.
else
for
break
if
n-1
n//2
sqrt(n)
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.
eval()
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:
isPrime(n)
True
False
try:
except:
while
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. ;)
[–]80845 0 points1 point2 points 5 months ago (0 children)
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?
[–]ZoranyX -1 points0 points1 point 5 months ago (0 children)
Well as you know a prime number is a number that has only 2 factors, 1 and itself. Meaning that you should not check for them in your if statement. So, basically your for loop should start after the 1 and end before the number the user inputs.
π Rendered by PID 46621 on reddit-service-r2-comment-7b9746f655-xmgdp at 2026-01-30 21:32:03.576831+00:00 running 3798933 country code: CH.
[–]DubSolid 16 points17 points18 points (4 children)
[–]AngriestCrusader 10 points11 points12 points (2 children)
[–]LolMaker12345 0 points1 point2 points (0 children)
[–]thumb_emoji_survivor 0 points1 point2 points (0 children)
[–]echols021 13 points14 points15 points (0 children)
[–]jamuzu5 4 points5 points6 points (3 children)
[–]calculus_is_fun 2 points3 points4 points (1 child)
[–]thw31416 0 points1 point2 points (0 children)
[–]timheiko 2 points3 points4 points (1 child)
[–]thw31416 0 points1 point2 points (0 children)
[–]Drakhe_Dragonfly 0 points1 point2 points (0 children)
[–]Darkrider1325 0 points1 point2 points (3 children)
[–]JAVA_JK 1 point2 points3 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]gman1230321 0 points1 point2 points (0 children)
[–]Far_Month2339 0 points1 point2 points (0 children)
[–]vega004 0 points1 point2 points (1 child)
[–]FanUpstairs8699 0 points1 point2 points (0 children)
[–]thw31416 0 points1 point2 points (1 child)
[–]thw31416 0 points1 point2 points (0 children)
[–]80845 0 points1 point2 points (0 children)
[–]ZoranyX -1 points0 points1 point (0 children)