i managed to get it up to this point.
after i finished this code i stopped getting syntax errors or any error from the guide it self on where it fails to function thus i have no idea where this code fails in.
any help would be appreciated
If you add print statements in your function you can print out what is happening step by step and compare that output to what you meant.
Also note that the else in the while-else only makes sense if you have a break statement in your loop, it’s the break statement that controls whether the else executes or not.
but then how would the break function be added into that code, would it mean that the said if statement would break after it reaches x < n?
break is a statement, not a function
You don’t have one. So I’m not sure what you mean.
Perhaps you mean that I mean that you should have a break statement. I did not say that. But I did say that if you don’t have a break, then you shouldn’t have a while-else
You might generally just want to forget loop-else exists until later, it’s not commonly used.
Usually it’s used for “check all these, break if found, else none found” – and yes, that fits the pattern of trial division, but you’re exiting the whole function instead, meaning that you have no use for loop-else.
Look. If you mean to do something after a loop, then you just put it after.
loop: stuff to repeat do some other stuff after loop
so the problem im getting is that if its not stated as a while-else statement then if the number the code is checking is 2, the result would end up in none since the condition to enter my while loop is that x is greater than n and since x = 2 and n = 2 the while statement is never entered or rather instantly fulfilled.
You should probably remove the while-else before you even … think.
I’m not using break -> there should be no while-else
If you mean that your function returns None, then no, it does not. That function will always reach a return statement, it will not exit by reaching past the last line of the function (that’s assuming that you’re not mixing tabs and spaces, which is information that your screenshot does not contain)
nvm on the 2 part i was just being an idiot apparently.
now im suddenly getting the x = 9 problem which i dont seem to understand
for what i understand what my code currently does is if x(9) > n(2) it would enter the while loop and check whether 9 is divisible by 2 and if not n would + 1 and try 9 divided by 3 and this would come up as true and return false but it says that it returns true instead
We’re not looking at the same code.
And it may be a case of mixed tabs and spaces.
A screenshot isn’t a byte-by-byte representation of the code, it’s better if you paste it.
Also, note that you have unreachable code (can’t do anything more after having exited the function). And, your while-loop isn’t a loop at all because the function exits on the first iteration.
Again, assuming you’re not mixing tabs and spaces as that would change the meaning of that code.
You count from 2 up to x.
You stop if you find a divisor, not prime.
If the loop finished, it’s prime.
You can use prints to check whether it’s doing that.
Print the current number,
print if you exit,
print if the loop finished.
That’s three print statements.
You’ll have output that’s easy to read and determine where it goes wrong.
but the function ends with the loop, there is no code after the loop? i hate my self for being a incompetent human being q.q
If you want to do more things after the loop, which you do because you need to know if the loop finishes, simply add code after the loop
Things that are to be repeated goes into the loop, things that are to happen after go after.
And, be paranoid about tab characters because that editor is not well-behaved. Preferably you’d use only spacebar (an editor should help with this mundane task, but that’s not the case here)
The thing about tab characters is that they mean “go to the next multiple of 8 spaces” but they can be displayed as 2 4 or 8 depending on editor settings, meaning that they can mean something other than what it looks like.
No, tab characters are not supposed to be a struggle, you’re simply supposed to use a sane editor, maybe even color tab characters in angry red so that if they somehow make it in you’ll know
And you might not even have them. It’s just that your description of what was going on, combined with the first screenshot, was suggesting them. But the second screenshot probably explains that.
actually i havent used any tabs iirc. Im pretty sure all of the indentions are from spacebars.
wouldnt the while loop auto finish once it can not fulfill the condition of x being greater than n?
Each time the loop reaches past its last line it’ll run the condition and either repeat the body, or continue with next statement after the loop, depending on the outcome of the condition
Your last screenshot shows a loop that isn’t a loop at all, because it either exits the function with True, or exits the function with False, it’ll never not exit and do another iteration. And, again, if something is supposed to happen after the loop, then that code shouldn’t be inside the loop
def f(): # pointless loop, doesn't repeat while True: return True
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.