A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • one year ago

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?

  • This Question is Closed
  1. rsmith6559
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 1

    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.

  2. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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?

  3. osanseviero
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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

  4. osanseviero
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    That won't fix all the problem, but you're really close.

  5. osanseviero
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Run the code and you'll see a LOT of text, but you will have a better idea of what is going on

  6. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  7. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  8. osanseviero
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Great!

  9. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.