Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1042539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:31:29+00:00 2026-05-16T15:31:29+00:00

I am learning Python now, and today, I met a problem in http://docs.python.org/release/2.5.4/tut/node8.html 6.1.1

  • 0

I am learning 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 the 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]))
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:31:29+00:00Added an answer on May 16, 2026 at 3:31 pm

    What exactly did you do in the shell? What is the code you are running?

    It sounds like you made a mistake in your script – perhaps missing the colon or getting the indentation wrong. Without seeing the file you are running it is impossible to say more.

    edit:

    I have figured out what is going wrong. You are trying to run python fibo.py 222 in the python shell. I get the same error when I do that:

    [138] % python
    Python 2.6.1 (r261:67515, Apr  9 2009, 17:53:24)
    [GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> python fibo.py 222
      File "<stdin>", line 1
        python fibo.py 222
                  ^
    SyntaxError: invalid syntax
    >>>
    

    You need to run it from the operating system’s command line prompt NOT from within Python’s interactive shell.

    Make sure to change to Python home directory first. For example, from the Operating system’s command line, type: cd C:\Python33\ — depending on your python version. Mine is 3.3. And then type: python fibo.py 200 (for example)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning python and PyGTK now, and have created a simple Music Organizer. http://pastebin.com/m2b596852
I'm learning how to use python right now. I found a problem while learning
I'm learning python for a couple of days now and am struggling with its
I have been learning python for some time now. While starting this learning python
I have been learning Python for a while, and now I'd like to learn
learning the basics of Python, now ready for a bigger challange... it didn't take
I have committed to learning C now, I'm good with Python/PHP/Bash but I've decided
I've been learning to program with Python on and off for a while now,
I'm still learning Python as great simple programming language, the problem came with DataBase
I've just started learning Python today. I've been reading a Byte of Python. Right

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.