python缩进问题

时间:2015-01-18 18:22:06

标签: python indentation

运行以下代码

  

检测到不一致的限制。

我无法理解原因。任何想法?

[这仅用于发布问题,因为它要求提供更多详细信息。]

class FSM():
    def sel(self):
        self.big=[]
        self.small=[]
        for i in cpop:
            if i.eve>goal:
                self.big.append(i)
            elif i.eve<goal:
                self.small.append(i)
            else:
                print('goal')
                break
    def cross(self):
        self.s1 = randint(0,len(self.big))#nradn
        self.s2 = randint(0,len(self.small))
        self.p1 = self.big[s1]
        self.p2 = self.small[s2]
        self.ch1=self.p1[0,2]+self.p2[2,len(p2)]
        self.ch2=self.p2[0,2]+self.p1[2,len(p1)]
        self.chs.append(ch1)
        self.chs.append(ch2)
    def npop(self):
        self.npop=[]
        self.F={}
        for i in range(0,len(self.chs)):
            self.F[i]= Per(self.chs[i], none)
            self.npop.append(F[i])
        self.F={}
        n+=1
    def cpop(self):
                if n==0:
                        x=Per()
                        z=Per()
                        k=Per()
                        self.cpop=[x,z,k]
                else:
                        self.cpop=self.npop

2 个答案:

答案 0 :(得分:1)

您需要在第二行缩进,您要在其中描述FSM

自:

class FSM():
def sel(self):
...

class FSM():
  def sel(self):
  ...

另外,请确保没有标签和空格的混合。

因为你有

def npop(self):
    self.npop=[]
    self.F={}
    for i in range(0,len(self.chs)):
        self.F[i]= Per(self.chs[i], none)
        self.npop.append(F[i])
    self.F={}
    n+=1
def cpop(self):
            if n==0:
                    x=Per()
                    z=Per()
                    k=Per()
                    self.cpop=[x,z,k]
            else:
                    self.cpop=self.npop

看起来cpop方法就是这种情况。

答案 1 :(得分:0)

试试这个缩进:

class FSM():
    def sel(self):
        self.big=[]
        self.small=[]
        for i in cpop:
            if i.eve>goal:
                self.big.append(i)
            elif i.eve<goal:
                self.small.append(i)
            else:
                print('goal')
                break

    def cross(self):
        self.s1 = randint(0,len(self.big))#nradn
        self.s2 = randint(0,len(self.small))
        self.p1 = self.big[s1]
        self.p2 = self.small[s2]
        self.ch1=self.p1[0,2]+self.p2[2,len(p2)]
        self.ch2=self.p2[0,2]+self.p1[2,len(p1)]
        self.chs.append(ch1)
        self.chs.append(ch2)

    def npop(self):
        self.npop=[]
        self.F={}
        for i in range(0,len(self.chs)):
            self.F[i]= Per(self.chs[i], none)
            self.npop.append(F[i])
        self.F={}
        n+=1

    def cpop(self):
        if n==0:
            x=Per()
            z=Per()
            k=Per()
            self.cpop=[x,z,k]
        else:
            self.cpop=self.npop