I am learn python now, and today, i met a problem in http://docs.python.org/release/2.5.4/tut/node8.html
6.1.1 Executing modules as scripts
When you run a Python module with
python fibo.py <arguments>
the code in the module will be executed, just as if you imported it, but with the name set to "main". That means that by adding this code at the end of your module:
if __name__ == "__main__":
import sys`
fib(int(sys.argv[1]))
you can make the file usable as a script as well as an importable module, because the code that parses the command line only runs if the module is executed as the "main" file:
$ python fibo.py 50 1 1 2 3 5 8 13 21
34
but when i do this in shell, i got
File "<input>", line 1
python fibo.py 222
SyntaxError: invalid syntax
how to execute script correctly?
fibo.py is
def fib(n):
a,b=0,1
while b<n:
print b,
a,b = b,a+b
def fib2(n):
result=[]
a,b=0,1
while b<n:
result.append(b)
a,b=b,a+b
return result
if __name__ =="__main__":
import sys
fib(int(sys.argv[1]))