6.00 PS1 - 1000th prime number Please take a look at my code. It's returning an infinite loop, and I don't understand why. http://ideone.com/0G6S9j I'm fairly confident that my range() function isn't doing what I want it to do. What I want is for lines 4-7 to run for ALL values within the range stated in line 4, before moving on to line 8 if they all return a False to 5. Also, sometimes my while function doesn't work properly, and I get runs that go way past primesLeft being negative. What's going on with that?
Your if/else statement shouldn't be inside your for loop. The for loop is determining primeness, and the if/else is printing the result of that and modifying trialnum too. Little trick: all non-prime numbers eventually factor to prime numbers.
Yeah, I considered a way to simplify my range as a list that keeps adding values for prime numbers as I find them. The issue is that I don't know much about lists yet, so I didn't want to use a method that would involve too much nuance or looking stuff up before it's taught in the lecture. I figured my method would get the same result, as well as match the assignment's hints more closely. I'm confused by your advice because when I make the change you suggest and dedent my if/else, I get an expected indent error ( http://ideone.com/xCE9xB). I also don't understand what you mean in terms of why the if/else shouldn't be part of the for. It seems my if/else is entirely contingent on the range set up by my for statement, as the if and the else both make use of the variable I set in the for. What is actually happening that's causing this infinite loop?
About the indent error: When you use python, you need to indent at the correct places. A for will repeat a block of code, and you have a if/else which I can guess that should be inside the for. So there should be indentation
That won't fix all the problem, but you're really close.
Run the code and you'll see a LOT of text, but you will have a better idea of what is going on
I'm sorry if I'm being unclear. I need to understand WHY I get an infinite loop. It's nice to know that I was right about my indents, but I don't understand what that has to do with my getting an infinite loop. Per these comments, I dedented my if/else., then redented them. Now my code looks exactly the same as my initial code in this thread, which yields an infinite loop. I need to know what I'm doing wrong.
I have resolved the issue. For those looking for the solution to this problem set, I encourage you to look into using booleans to verify whether or not the condition "every item in the range returns a remainder" is met.
Great!
Join our real-time social learning platform and learn together with your friends!