-4

I'm trying to get the primes out of it, and it's behaving strange, it's not doing what it's saying...can't figure it out, could it be the indentation? This code return just 0 and 9 as primes. It says it's removing 9 but it's not. But it's removing the others... http://ideone.com/QDnLyo

def p6():



for x in range(2,10):
     myPrimes = set()
     myDividersList = set()

     for y in range(2,10):

        if (x%y != 0 and x != y): #if modulo is diff than 0 add it to the Primes list
            myPrimes.add(x)
            myDividersList.add(y)
            print ('I added',x,y,'to the lists')

        else :
            print (x, 'does equally divide with', y, ' and I did not put' ,x, 'on the list')
            break

        if (x%y == 0 and x in myPrimes): #if x is already in the list take it out
            if x!=y:
                myPrimes.remove(x)
                print (x, 'is in the list and its not prime so I removed it')
            else: break


return[print('These are my possible primes:',list(enumerate(myPrimes)), 'and these are my dividers', list(enumerate(myDividersList)))]



compilation info
Traceback (most recent call last):
File "/usr/lib/python3.2/py_compile.py", line 119, in compile
optimize=optimize)
File "./prog.py", line 22
return[print('These are my possible primes:',list(enumerate(myPrimes)), 'and these are   my dividers', list(enumerate(myDividersList)))]
^
SyntaxError: 'return' outside function

Return it's ok...wait a sec, to get the way it works through an outside ideone link

I got to this with the rubber duck:

def p6():
for x in range(2,10):
     myPrimes = set() #define my primes set
     myDividersList = set() #define my dividers set

     for y in range(2,10):
        if (x%y != 0 and x != y): #if the modulo is diff than 0 and x is diff than y
            myPrimes.add(x) #add x to the list
            myDividersList.add(y) #add y to the list
            print ('I added',x,'and',y,'to their lists') #print that I added x and y to their lists

        elif (x%y == 0 and x in myPrimes): #else, if the modulo is equal to zero and x is already in myPrimes
            myPrimes.remove(x) #remove it from the primes
            print (x, 'is in the list and its not prime so I removed it')

return[print('These are my possible primes:',list(enumerate(myPrimes)), 'and these are my dividers', list(enumerate(myDividersList)))]

http://ideone.com/ipv91U

Andra
  • 39
  • 1
  • 8
  • 1
    what is the error you get is it a indentation error? – K DawG Dec 25 '13 at 15:08
  • 3
    The indentation in your code is incorrect. Is this how your actual code looks? – Totem Dec 25 '13 at 15:09
  • yes...how should I put it? – Andra Dec 25 '13 at 15:14
  • I'm not sure it's an indentaion error. I don't figure out something wrong about the logic behind of it, just that it doesn't put the correct primes to the list when it returns the function. For instance, at this point I get 0 and 9 as primes – Andra Dec 25 '13 at 15:17
  • what do yo expect, and what do you get ? "Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance." – bruno desthuilliers Dec 25 '13 at 15:17
  • 1
    Also did you try to run your code with a debugger ? Or the rubber duck debugging trick ? – bruno desthuilliers Dec 25 '13 at 15:18
  • I tried, it says it's ok... Can't figure it out just yet. I will try the rubber duck :D – Andra Dec 25 '13 at 15:40
  • 1
    I guess you need to start reading about python syntax basics first. How to indent functions and loops etc... – FallenAngel Dec 25 '13 at 15:40

1 Answers1

1

Just to answer your question about how the code should be indented

You code looks like this:

def p6():

your code is here # this is wrong

It should be like this:

def p6():
    code is here
    #All code in your function should be indented at least this much
    #These comments are within the function
    #Indentation of 4 spaces is convention, and while no exact distance is strictly required, indentation must be at the very least consistent

#Code with same indentation as the def p6() declaration above is not within the function
#So this comment is NOT within the function

It's the same with a loop

for i in range(x):
    #Code here is within the for loop

#Code indented like this is NOT within the for loop by virtue of it's indentation

if i == x:
    #Within the if
#Not within the if

As you requested here is a for loop containing an if-else, to display the correct indentations:

for i in range(x):
    if i > y:
        #code indented like this
    else:
        #code also indented like so

Notice that the if and else statements are indented 4 spaces inside the for loop. Likewise, the code inside the if and else is indented by 4 spaces. Let me know if this makes sense to you.

Totem
  • 6,563
  • 4
  • 32
  • 60