newbie question

J

Julia Osip

Hi, just started fiddling around with python, having some difficulty
getting the following small play program to execute. Furious
searching in the docs, manual, newsgroup did not avail me, does anyone
have any ideas what might be the problem? Some sort of whitespace
issue (im still unsure of whitespace rules)? I'm running Mac OS
10.2.8, with MacPython 2.3.

Thanks for any help you can give...

the code
....snip...
#!/usr/bin/env python

class World:

"""contains everything"""

def __init__(self):
self.name = "world"
self.locations = [] # list of all locations in
the world
self.dist_locations = [] # the amount of travel time required
between locations
self.actors = [] # list of all actors in the world
self.organizations = [] # list of all organizations in the world
self.time = 0 # current moment in the world
return

....snip...

the error
....snip...
File "<string>", line 7
def __init__(self):
^
SyntaxError: invalid syntax
....snip...
 
S

Sean 'Shaleh' Perry

Hi, just started fiddling around with python, having some difficulty
getting the following small play program to execute. Furious
searching in the docs, manual, newsgroup did not avail me, does anyone
have any ideas what might be the problem? Some sort of whitespace
issue (im still unsure of whitespace rules)? I'm running Mac OS
10.2.8, with MacPython 2.3.

you might consider joining the (e-mail address removed) mailing list. It is meant to
help people new to python.
Thanks for any help you can give...

the code
...snip...

the error
...snip...
File "<string>", line 7
def __init__(self):
^
SyntaxError: invalid syntax
...snip...

As you guessed it is a whitespace problem. So, let's clear up the whitespace
rules for you.

There is one key rule -- be consistent. Python does not really care if you
use 4 space indents or 8. It does care if you try to use both. Same goes
for mixing spaces and tabs. As Python reads your code it figures out what
your indent style is and then enforces that for the rest of the block.

When Python read your class definition the first indentation it found was for
the docstring which was 4 spaces. Then it went to the next line of code and
found an indentation of 8. This is what caused the error. Now, maybe this
is because you hand indented the docstring by four spaces and then used a tab
on the line starting with 'def'. Or maybe you thought you needed to indent
again (you don't).
 
S

Sean 'Shaleh' Perry

Try getting rid of the 'return'
Norm

you are right, the return here is not needed.

A return statement is only required when:

a) you want to leave a function early

b) you actually want to return something
 
C

Colin J. Williams

You might look at your indentation.

The def __init__ line should have the same indentation as the preceding
doc string.

Colin W.
 
J

Julia Osip

Didnt realise I was posting into an existing topic. Whitespace was
the problem, its working now. Will consider the mailing list in the
future. Thanks very much for your help!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,174
Messages
2,570,940
Members
47,486
Latest member
websterztechnologies01

Latest Threads

Top