[Tutor] New to Python

Steven D'Aprano steve at pearwood.info
Fri May 18 09:10:29 CEST 2012


Hi Jeremy,

Welcome to Python, but please don't send HTML email (what some programs 
wrongly call "Rich Text"), as it completely destroys the necessary indentation 
of your Python code and makes it difficult or impossible to work out what your 
code is supposed to be.

However, I will take a wild guess as to what your problem is. You wrote:

> I'm running this on mac 10.5.8. When I run this script from the command
> line, there is no file output. However, if I remove the "def" statement in
> the script and run again from the command line, a test.txt file is output.
> I guess I'm trying to understand why using the def statement changes how
> the script operates and since it does, how do I correct this code so it
> runs from the command line with the def statement.

The "def" statement defines a function. The function doesn't run until you 
call it.

For example:

# Define a function.
def test():
     print("Hello world")

# Nothing gets printed here, since the function is only defined,
# but not yet called.

# Now actually call it.
test()  # Take note that you need the round brackets () to call it.

"Hello world" will be printed.


Does that help?





-- 
Steven


More information about the Tutor mailing list