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)))]