Why am I getting a syntax error on this code

Python ver 3.9.4

msg = "I love learning to use Python."
print(msg)

Thank you, new to Python, kinda new to coding, sorry if i posted in wrong area

I’m not sure, because copy/pasting your code directly into the interactive prompt works just fine for me :slight_smile:

Rather than summarizing what went wrong as “a syntax error” it’s usually best to copy/paste exactly the code that you used and the error you got along with a description of how you ran the code so that others can see what you saw and give better help. Barring that, the best I can do here is “try again, it ought to work”.

Welcome to Python :). You posted in exactly the right place.

I’m not sure, because copy/pasting your code directly into the
interactive prompt works just fine for me :slight_smile:

Maybe the OP hand typed correct code into the forum :slight_smile: While reading by
eye from incorrect code :frowning:

Rather than summarizing what went wrong as “a syntax error” it’s
usually best to copy/paste exactly the code that you used and the error
you got along with a description of how you ran the code so that others
can see what you saw and give better help. Barring that, the best I
can do here is “try again, it ought to work”.

Aye. In particular, note that Python quotes are either ASCII double
quote " or ASCII single quote '. Sometimes people type some kind of
“smart quote”, cut/paste from an badly transcribed example. For example,
the Unicode open and close quotes (distinct characters) are not valid
Python quotes.

Cheers,
Cameron Simpson cs@cskk.id.au

Sometimes the Python interpreter gets confused about errors on the
previous line, and reports the syntax error in the next line:

# Python 3.9
>>> x = [1, 2, 3
... msg = "I love learning to use Python."
  File "<stdin>", line 2
    msg = "I love learning to use Python."
    ^
SyntaxError: invalid syntax

The actual error is the previous line, the x = line is missing a
closing bracket.

scott,

try putting everything into a main function. i tried this and it worked first try:

def main():
msg = (‘i love learning to use python’)
print(msg)

return

main()

cameron, another noob here. i read your response to this post and you mention the interactive prompt. are you referring to the command line interpreter or is this something else?

cameron, another noob here. i read your response to this post and you
mention the interactive prompt. are you referring to the command line
interpreter or is this something else?

For me, the interpret invoked interactively from the command line.
Example:

CSS[~/hg/css-pypi(hg:pypi)]fleet2*> py3
Python 3.9.10 (main, Jan 15 2022, 11:48:15)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more 
information.
>>>

In the above, CSS[~/hg/css-pypi(hg:pypi)]fleet2*> is my shell prompt,
which is what I mean if I say “the command line”. For commands like
ls, rsync, etc.

The string py3 above is me typing the command:

py3

which is a little shell script of my own to invoke my “current”
preferred python, usually from a personal virtualenv. It ends up running
python3. With no arguments, that’s the Python 3 interactive mode:

Python 3.9.10 (main, Jan 15 2022, 11:48:15)
[Clang 12.0.0 (clang-1200.0.32.29)] on darwin
Type "help", "copyright", "credits" or "license" for more 
information.
>>>

The >>> above is the Python interactive prompt, where you can type
Python syntax. That’s what I mean by “the Python interactive prompt”.

Cheers,
Cameron Simpson cs@cskk.id.au