[Tutor] Need to write a python and call it within a python mainprogram (fwd)

Alan Gauld alan.gauld at freenet.co.uk
Sat Feb 18 23:56:53 CET 2006


> Using it,  I translate my pseudo code
> into

>>> def trans01(a,b,c,d):
....     j = 8 * a%2 + 4 * b%2 + 2 * c%2 + d%2
....    if j = 0,  a,b,c,d = a/2,b/2,c/2,d/2
....   if j = 1,  a,b,c,d = -1,-1,-1,-1
....   if j = 2,  a,b,c,d = a,b/2,c,d/2
IndentationError: expected an indented block (<pyshell#0>, line 2)

> Why did I get this error message?

Indentation is how python determines block sytructure.

In C you would do:

void f(int x)
{
    /// some code here
}

and the {} tell C that the code between them ids the function bvody

In Python a colon says a block is coming up and everything up to the
next unindented line is part of it. But the indentation must be consistent
(it can be as big or little as you like, but it must be consistent - 2, 3 or 
4
spaces is pretty normal)

In your case the body of the function had inclonsistent indentation.

The other issue is in the format of your if statements:

....    if j = 0,  a,b,c,d = a/2,b/2,c/2,d/2

You need to use == as in C for comparisons, unlike C you cannot
do an assignment in a test. The condition must be followed by a colon.
You test thus becomes:

if j == 0: a,b,c,d = a/2,b/2,c/2,d/2

Note the double equals and the colon.

HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list