Chris
Chris

Reputation: 22277

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.

    def 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"

Upvotes: 3

Views: 1571

Answers (8)

Jackson Miller
Jackson Miller

Reputation: 1510

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

Upvotes: 0

cquillen
cquillen

Reputation: 1347

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.

Upvotes: 2

Seth
Seth

Reputation: 46473

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.

Upvotes: 3

brettkelly
brettkelly

Reputation: 28245

def __init__(self):
    #stuff.

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

def __init__(self):
    #stuff.
    pass

Upvotes: 2

Fred Larson
Fred Larson

Reputation: 62123

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

Upvotes: 2

Kyle Lutz
Kyle Lutz

Reputation: 8046

Change:

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

To:

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

and so on for all the methods.

Upvotes: 2

CB Bailey
CB Bailey

Reputation: 793249

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

Upvotes: 4

user44484
user44484

Reputation:

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.

Upvotes: 11

Related Questions