A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

lucifer

  • 4 years ago

Can someone explain what is wrong with this code that I get an error. I want to print the primes between 3 and 11. eventually I want to find the 1000th prime for pset01.

  • This Question is Closed
  1. lucifer
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    here is my code

    1 Attachment
  2. maitre_kaio
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Explain the logic of this code. I see you're trying to do it recursively, but I need help to understand.

  3. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    http://pastebin.com/XRvYy0zu Here is a quick revision of your code. Take a look at the changes I made to it. You will be able to enter a number there and find it to be prime or not.

  4. lucifer
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Thank you, I can see why your code would work mortir, but I'm curious what was wrong with mine. Here is the logic

    1 Attachment
  5. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    i get this traceback http://dpaste.com/695033/

  6. lucifer
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I know. what does this mean? what should I do to my code?

  7. maitre_kaio
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    I still don't understand what you're trying to do. But this error message means you are trying to use the variable i before having given an initial value to it. You have to initialize i before the while loop.

  8. lucifer
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    But I said i = 3 -- first line.

  9. maitre_kaio
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Warning: I am *not* saying it's good programming style. I'm just explaining where does this error comes from. Check this : http://www.saltycrane.com/blog/2008/01/python-variable-scope-notes/ You are using a variable named i inside a function, and another variable named i is defined in the global scope. If you want the interpreter to know that, inside the function, you are setting the global variable, you have to say it this way: global i If you do that, you have another error, not related to the first one: TypeError: unsupported operand type(s) for +: 'int' and 'str' Easy to fix by putting str() around i in the print statement. After that the program seems to run: http://pastebin.com/VS8kYf56 Doesn't produce good results though.

  10. lucifer
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    sigh ... I know it somehow thinks 9 is prime. Now this is my slightly better attempt using morfir's code -- explanation provided of course. The only problem is, it refuses to find ith primes for i>100. otherwise, it works fine. heh -- the wonderful world of incompetent newbie programming... please, can you explain what that error is. How can I understand what these errors mean in general -- maybe if I can see visually the program running line by line...

    1 Attachment
  11. lucifer
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    wohooo... done it 7919 it was a small matter of increasing the recursion limit. did everyone do that or just me?

  12. anonymous
    • 4 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    http://docs.python.org/reference/executionmodel.html don't think i tackled this one with recursion

  13. 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.