开发者

Python indentation issue?

I'm pretty new to python. This is my first time working with classes in python. When I try to run this script, I get

IndentationError: expected an indented block

What is wrong with this?

import random

class Individual:
    alleles = (0,1)
    length = 5
    string = ""

    def __init__(self):
        #some constructor work, here.

    def evaluate(self):
        #some stuff here.

    def mutate(self, gene):
        #mutate the given gene.

    def onePointCrossover(self, partner):
        #at some random point, crossover.

    def twoPointCrossover(self, partner):
        #at two random(?) points, crossover.

class World:
    def __init__(self):
        #stuff.

    d开发者_如何学JAVAef buildPopulation(self):
        #stuff.
        for individual in self.population():
            for i in range(0, individual.length):
                print random.random()


    def display(self):
        #print some output stuff.

if __name__ == '__main__':
    print "hi there"


All of those methods that consist of just a comment.

To fix it, for example, do this

def twoPointCrossover(self, partner):
        #at two random(?) points, crossover.
        pass

The comments don't count as compilable statements, so you have a bunch of empty blocks. That is why it gives you the indent error.


If you use something that ends in : expecting an indented block and you don't have anything that you want to put there (other than a comment) then you need to use pass.

E.g.

def doNothing(self):
    pass


When you're just outlining your classes and have a bunch of methods which do nothing, you need to insert the pass statement to indicate that nothing is happening.

Like so:

class Individual:
    alleles = (0,1)
    length = 5
    string = ""

    def __init__(self):
        #some constructor work, here.
        pass

    def evaluate(self):
        #some stuff here.
        pass
    ...

The unexpected indent message is because python is looking for an indented statement to follow the method definition.


Change:

class World:
    def __init__(self):
       #stuff.

To:

class World:
    def __init__(self):
        #stuff
        pass

and so on for all the methods.


Unless you're abbreviating your code for this post, you'll need pass after all of those functions that don't have any code.


def __init__(self):
    #stuff.

That looks wrong at first glance. Try changing it to this:

def __init__(self):
    #stuff.
    pass


Double check the tabs and spaces in all the code, make sure you are not mixing them. A line with several spaces may the same as a line with a single tab.


Others have covered pass, so I will just add that for beginning python programmers, it can take some getting used to the importance of whitespace.

Until you get used to it you might want to get in the habit of converting tabs to spaces or spaces to tabs when you save a file. Personally I prefer tabs because it is easier to tell the difference if it is off by one (especially at the beginning/end of a nested block).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜