这个Python for循环列表代码有什么问题?

时间:2013-12-25 15:07:40

标签: python python-3.3

我试图从中获取素数,并且它表现得很奇怪,它没有做它所说的......无法理解它出来,这可能是缩进吗?此代码仅返回0和9作为素数。它说它已经删除了9但它没有。但它正在删除其他人...... 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

让它恢复正常......等一下,通过外部的ideone链接获得它的工作方式

我用橡皮鸭做到了这一点:

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

1 个答案:

答案 0 :(得分:1)

回答你关于如何缩进代码的问题

您的代码如下所示:

def p6():

your code is here # this is wrong

应该是这样的:

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

循环

是一样的
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

正如您所要求的那样,是一个包含if-else的for循环,以显示正确的缩进:

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

请注意,if和else语句在for循环中缩进了4个空格。同样,if和else中的代码缩进4个空格。如果这对您有意义,请告诉我。