How hard can this be?

Remco Gerlich scarblac at pino.selwerd.nl
Mon May 21 11:32:14 EDT 2001


Patrick Kirk <pknews at kirks.net> wrote in comp.lang.python:
>   if x > 0:
>     print "x is positive"
> 
> You would think anyone could enter that but I can't.  I get the following:
> 
> >>> import math
> >>> if x > 0:
> ...  print "x is positive"
> ...
> Traceback (innermost last):
>   File "<stdin>", line 1, in ?
> NameError: x

You entered it correctly, but x doesn't exist yet. You need to give it a
value first (ie, x = 4), otherwise Python gives a NameError, indicating that
the name 'x' is unknown to it.

> I've tried creating a .py file saying:
>   if x > 0:
>     print "x is positive"
> and importing it but that doesn't work either.
> 
> >>> import a
> Traceback (innermost last):
>   File "<stdin>", line 1, in
>   File "a.py", line 1
>     if x > 0:
>     ^
> SyntaxError: invalid syntax

In this case, you put spaces in front of the if. Since Python uses
whitespace to know where blocks begin and end, that if has to be right at
the beginning of the line.

If it had worked, Python would have complained about the unknown x again.

-- 
Remco Gerlich



More information about the Python-list mailing list