[Tutor] New to Python - print function - invalid syntax
Steven D'Aprano
steve at pearwood.info
Tue Aug 5 04:38:05 CEST 2014
On Mon, Aug 04, 2014 at 04:44:46PM -0700, Greg Markham wrote:
> Ok, when I try this from the Shell window, it works. When executing the
> full program from command line, it does not. Python versions from both
> shell and command line are 3.4.1 (confirmed via command: "python -V").
I'm a little confused, because I consider "the shell" and "command line"
to more or less be synonymous. Perhaps if you explain step-by-step what
you do. Here's my guess of what you mean by "command line":
Click on Start Menu.
Choose "Run" command.
Enter "cmd.exe"
Enter "python I:\Programming\Scripts\Learning\chapter02\game_over2.py"
Am I close?
> Full error msg output when run from cmd line reads:
>
> File "I:\Programming\Scripts\Learning\chapter02\game_over2.py", line 14
> print("Here", end=" ")
> ^
> SyntaxError: invalid syntax
That definitely looks like a Python 2 error, but it should work in any
version of Python 3. This is a most perplexing error, I can only imagine
that you have both Python 2 and 3 installed and somehow, for some
unknown reason, you're sometimes getting one and sometimes getting the
other.
> > 2. copy and paste the code and complete traceback. retyping things
> > make it more likely that what is in your code isn't exactly what you
> > typed
> >
>
> Apologies, but what is a traceback? Is this a debugging feature found in
> the shell? (I looked, but didn't see it)
A traceback is the text Python prints when an error occurs. It normally
starts with the word "Traceback" and ends with the kind of error
(SyntaxError, NameError, ImportError, etc.) and usually an error
message. For example:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 2, in function
TypeError: object of type 'int' has no len()
or similar. The details will differ slightly (e.g. sometimes Python can
print the offending lines of code) but the important thing is that there
is a lot of useful information available in the traceback, if you show
us the entire thing, from start to finish.
SyntaxError is quite exceptional, in that it normally doesn't start with
the word "Traceback", for technical reasons[1]. Technically, I suppose,
it's not really a traceback, but we tend to call it such regardless.
[1] SyntaxError normally occurs at compile-time, while Python is parsing
the source code, and before execution starts, so there's no chain of
calling functions and no traceback. But we normally don't care about the
technical details.
--
Steven
More information about the Tutor
mailing list