2

This is my first day learning programming. I'm following Python Programming: An introduction to computer science 2nd ed. by John Zelle, and so far things have been going smoothly.

The only trouble is that when I try and import a saved program I get a syntaxerror. I write the program and save it before executing, but then when I try to import it I get the error. I tried opening a fresh instance of the shell but no cigar. I'm using OSX Lion 10.8 and Python 2.7.3. Any help is appreciated. This is what the problem looks like:

>>> #File: chaos.py
>>> #A simple program illustrating chaotic behavior.
>>> def main():
    print "This program illustrates a chaotic function"
    x=input("Enter a number between 0 and 1: ")
    for i in range(10):
        x = 3.9 * x * (1-x)
        print x


>>> main()
This program illustrates a chaotic function
Enter a number between 0 and 1: .25
0.73125
0.76644140625
0.698135010439
0.82189581879
0.570894019197
0.955398748364
0.166186721954
0.540417912062
0.9686289303
0.118509010176
>>> import chaos

Traceback (most recent call last):
  File "<pyshell#47>", line 1, in <module>
    import chaos
  File "chaos.py", line 1
    Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43)
             ^
SyntaxError: invalid syntax
1
  • Could you post the contents of chaos.py or at least the first line which has the syntax error? Commented Feb 17, 2013 at 17:31

2 Answers 2

1

My guess is that you are copying the contents of the terminal to the file, verbatim. And there are a lot of thing that should not be there, that includes the version prompt.

The file should have just something like:

#File: chaos.py
#A simple program illustrating chaotic behavior.
def main():
    print "This program illustrates a chaotic function"
    x=input("Enter a number between 0 and 1: ")
    for i in range(10):
        x = 3.9 * x * (1-x)
        print x

No >>>, no ..., no tabulators and certainly do not copy the version information:

Python 2.7.3 (default, Dec 22 2012, 21:27:36) 
[GCC 4.7.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
Sign up to request clarification or add additional context in comments.

4 Comments

rodrigo, why aren't I able to delete the version prompt etc when I open IDLE? I'm trying to paste your solution to check if it will work and i'm not able to delete the version prompt and the >>>
@user2080913: You are confusing the IDLE Shell window with a regular text editor. To be fair IDLE doesn't make the distintion quite easy... You have to open the .py file with a proper editor (IDLE has an editor, but it is not started by default), edit out the extra lines and format the code appropriately.
OK rodrigo, I opened IDLE's text editor (or at least I think I did, by going to file>new) and copied and pasted the revised program into that, saved it, and opened the IDLE shell window to try and import the program. This is what happened: Python 2.7.3 (v2.7.3:70274d53c1dd, Apr 9 2012, 20:52:43) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "copyright", "credits" or "license()" for more information. >>> import chaos >>> main() Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> main() NameError: name 'main' is not defined >>>
@Victor: Almost there now than the module loads fine. In Python everything is namespaced so you have do import chaos and then chaos.main(). Or alternatively, from chaos import main and then simply main(). Or if you feel lazy: from chaos import * and main().
1
  File "chaos.py", line 1
    Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43)
             ^
SyntaxError: invalid syntax

It looks like the first line of your chaos.py script has a line which is not python:

Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43)

It should be removed or commented-out by starting the line with a # sign.


Some tips to keep in mind:

  • In Python, whitespace is important -- they indicate indentation level. Do not mix spaces and tabs lest Python raise IndentationErrors.
  • In texts or web pages, you may see transcripts of interactive sessions which include >>> or ... indicating the Python prompt or indentation level. If you transfer the code to a script, you must remove those.

3 Comments

OK thanks for the help guys. I'm learning a bunch. I removed that snippet, as well as the ">>>", and inserted a # sign at the beginning, and now I get this: >>> import chaos Traceback (most recent call last): File "<pyshell#37>", line 1, in <module> import chaos File "chaos.py", line 1 ">>> def main(): ^ SyntaxError: EOL while scanning string literal >>>
The error is saying that you have a quotation mark (probably either " or ') which is not closed. Perhaps you should delete chaos.py, open a new file and cut-and-paste the code that @rodrigo posted, and save that as chaos.py. Then you should be good to go.
@rodrigo I just download Xcode and i'm very overwhelmed at the vast array of menu items...which one do I choose to write my Python programs in, just a blank document? Or should I select "Shell script"? So if I have this right, I write my programs in Xcode or textedit or whatever, and then run them in the IDLE shell by importing them in there....is this right? Sorry for all the very basic questions, i'm having a difficulty understanding the conceptual framework. I'd appreciate any beginner resources you could point me to, as none have so far been very illuminating

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.