PDA

View Full Version : Python Programming




FDomin16
Apr 7, 2013, 10:55 PM
I'm new to programming and I decided to begin with Python. I downloaded Python 3.3 and I'm using the book "Python Programming: An Introduction to Computer Science 2nd edition". In the book there is an exercise to make a module file. I wrote the file in IDLE and I saved it as chaos.py in my documents. After I open a new IDLE and I try to import the file it comes up with this:

>>> import chaos
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import chaos
File "./chaos.py", line 1
Python 3.3.1 (v3.3.1:d9893d13c628, Apr 6 2013, 10:32:10)
^
SyntaxError: invalid syntax

As I said before, I am new to programming and I don't know what to do.



ytk
Apr 7, 2013, 11:27 PM
Post your code.

stueee123
Apr 8, 2013, 12:17 AM
What I always did when i used IDLE was to first save it as a .py file like you did, and then just double click it in Finder and select "Run"

Why are you trying to import it? just run it

estorstenson
Apr 8, 2013, 10:59 AM
It's been years since I used idle, but my guess is that it saved that line you see (above the ^ character) when you saved the session to a file and that line (and possibly others) are not valid python syntax.

Open the file you created inside a plain text editor (not something like word) and remove or comment any line that wasn't part of what you were supposed to type in the previous exercise. Once you save it, then it should work just fine.

If you have text that gets printed out by your program, you might want to use terminal.app instead of double clicking to run the script. As a programmer, it is always good to learn to use the terminal. It can be found /Applications/Utilities/Terminal.app. As a profession developer, I have it set to stay inside my dock (and it opens up after I restart). I spend roughly 50% of my work day inside the terminal.