[very new] invalid syntax error?

Hey there, I am trying to learn Python using this article: https://pythonistaplanet.com/python-basics/

But some of the examples do not work. See my screenshots:

Article:
edit: I had a screenshot of the article here but can’t apparently post this because I am a new user. So, you’d have to look that up from the link above. It’s at around 1/3 of the article.

Tried with Python 3.10 and IDLE Shell 3.10.7 (I haven’t found out the difference in purpose yet)

I’m sure i’m missing something but I am entirely new to this and stuck. Tried to contact the writer of the article some weeks ago but received no response.

Hi Nexlit,

This issue was found here:

IDLE and the interactive interpreter both wrongly reports a SyntaxError if no blank line after function definition. · Issue #93307 · python/cpython · GitHub

A blank line must be entered to indicate that the definition is complete.

2 Likes

Okay, thank you.

I understand what the issue is now and how to prevent this from happening. Although maybe not exactly why it works like this but hopefully that logic becomes apparent when I understand the language better.

Could you maybe take a guess at why the writer of the article didn’t put the blank lines in his examples?

The article seems to be very well written and correct otherwise.

Because, as was pointed out, this is a problem with IDLE and the writer did not use IDLE.

I understand what you’re saying but I am not sure how you conclude this. At the start of the article he provides a link to download the software, which i used to do so and in that download is IDLE. If he did not use IDLE than what did he use?

I haven’t really gotten to a part about the various software packages associated with Python in the tutorial yet.

Should I use something else to test out the examples given in the tutorial? If so, what?

There is no problem in your code and the problem is due to the limitation of REPL(IDLE).

# main.py
def greetings(): 
    print('Hey') 
    print('Hellooooo')
greetings()

If you want to run the code as it is, paste it to the text editor, save it as a text file (e.g. “main.py”), and run it with python. If you are using IDE such as VSCode, you can run it directly from the IDE.

From the command prompt type:

> python main.py

The last reply is above my head. Sorry.

I don’t know the differences between IDLE, IDE, VSCode and what exactly is defined by “run it with python”, yet.

I am still trying to learn the basics of the language at this point and have not paid any attention to the software beyond downloading it from the link provided. I just wanted to play with the examples from the articles to try and understand them. Maybe I should learn about the software before I learn the language?

I don’t think so.
A python script file is just a text file and can be created using any text editor.

I meant by “run it with python” that Python executes the code written in the text file.
(I don’t know better English expression)

Yes, REPL or IDLE is exactly for that purpose. So, what you have to do is to add just one blank line to the author’s code. :slightly_smiling_face:

I get adding the blank line.

I just don’t quite understand why the author didn’t add that line to begin with and then it was pointed out that he didn’t use IDLE but not what he used instead of IDLE. It’s also not explained in the article. The link in his article only has two executables; IDLE and Python 3.10 and both have a problem when the blank line is missing so I just wondered what to use if not either of the two from the download link.

I understand adding the blank line solves the problem in these two pieces of software I just wondered if there is a third piece of software that I should use that doesn’t have the missing blank line issue, that was also use by the author of the article.

I hope I made myself clear? Just explain it to me like I’m 5 years old. :wink:

I get adding the blank line.

In that it means: “ok, I’m done supplying this chunk of code”?

It is entirely a thing for IDLE’s interaction, where the user is typing
code line by line. When that code is an indented chunk of lines then
another code line is always acceptable, so you need a blank line to
indicate, “no, I’m not adding more lines, pleae run things now”.

I just don’t quite understand why the author didn’t add that line to begin with and then it was pointed out that he didn’t use IDLE but not what he used instead of IDLE.

I haven’t checked out what you were watching, but likely he just had
ccode in a file somewhere. There’s no need to include gratuitious blank
lines in Python code itself.

Personally, I usually use interactive Python (IDLE or things like it)
for very simple tests of things. Usually instead I have my programme in
a text file, and a separate window where I run it. So i might have some
code:

 for x in range(5):
     print(x)
 print("final x =", x)

in some file “foo.py”, and another window where I run it like this:

 python3 foo.py

There are no blank lines in the foo.py file because it is not being
typed interctively. Maybe the author was working that way.

Besides, with the code in a file you can go back and make changes easily
instead of retyping the whole thing.

Cheers,
Cameron Simpson cs@cskk.id.au

Okay so, the two executables from the link in the article are what we call “interactive python”? And those need the blank lines?

And there is also a non-interactive Python that doesn’t need the blank lines?

If that is correct then what I am looking for is how to use this non-interactive python. Do I write the code into a simple txt.file, save it as x.py and then what? How do I run it? When I click on a *.py file I see a window for one nanosecond and then it disappears and nothing else happens.

Yes.

Actually, python executes your file, but by the time the light leaves the display and reaches your eyes, the following message on the console must have appeared and then disappeared:

Hey
Hellooooo

If you want to keep the results, you’ll have to use the command prompt (assuming you’re on Windows).

  1. Launch command prompt.
    Enter “cmd” in the Windows search box (#-s), and select “command prompt”.

  2. Type:
    > cd (your workdir)
    “(your workdir)” is the directory where your ‘x.py’ is located.
    cd is the command meaning “change directory”.

  3. Type:
    /your workdir/> python x.py

Good luck!

2 Likes

Just to this last bit. In Kazuya’s example, /your workdir/> is the
prompt in the cmd window. And:

 python x.py

is what you would actually type.

Cheers,
Cameron Simpson cs@cskk.id.au

Not it is not. Python is a statement-oriented language, not a command-line language. In interactive mode, it requires a blank line to mark the end of compound statements. (For consistency and simplicity, this includes single-line compound statements like if 1: pass.) Otherwise, the interpreter does not know whether an un-indented line is missing 1 or possibly more indents or is the beginning of the another statement. It refuses to guess and raises the error. Note that in a command-line terminal, users must explicitly enter all indents, and it is easy to accidentally hit too early.

IDLE uses the standard library code.InteractiveInterpreter class, which intentionally imitates Python’s interactive mode. However, It adds indents for the user, so a user is less likely to accidentally enter a line with no indents. Given some other changes, IDLE might guess to interpret a no-indent line like “greetings()” the same as a blank line, and execute the previous code. But then users would not learn to enter blanks and would have trouble when using a standard terminal. I think it better to be consistent.

I am thinking about changing IDLE’s Shell to put syntax error messages in a popup box, like it does in editors. This would allow augmenting the message with more explanation and in this case, ask whether the unindented line is missing an indent or whether the user forget the blank line and wants the line to begin a new statement. IDLE could then fix the code as requested. I added a draft issue to the IDLE Issues project.

2 Likes

I’m slowly starting to understand things. Thanks to all who replied to this. A standard terminal, is that more or less equivalent to writing code in notepad or a similar text editor? While IDLE is interactive because it executes lines of code as you put them in not after executing your entire script?

This might be where I started to misunderstand stuff. To put it in context: I am not a coder, yet, but I have ‘written’ code and when I say that I mean that I’ve written stuff in, for instance, VBA in Excel and in Autohotkey using parts of already written code and editing that with the help of Google searches to make it fit my various needs. So I understand the concept of how programming languages are structured but I’ve never bothered to look beyond that, which I am trying to now by learning Python form scratch. I want to be able to come up with my own solutions, so to say.

The Autohotkey stuff I ‘wrote’ was written in notepad and then saved as .ahk and executed. But when I started the Pythin tutorial mentioned in my first post I was referred to downloading the Python software so I assumed I needed that specific software to be able to write and execute Python scripts. But apparently that’s not the case and it’s in fact similar to the Autohotkey stuff. I simply didn’t think of that.

Now as for having that interactive mode in IDLE, I have yet to understand what the advantages of that are exactly but I’m sure that will become apparent once I get deeper into Python.

I get the impression that you have never used interactive Python started from Windows Command Prompt. Perhaps you should read parts of Using Python. If you installed py.exe with python, enter py after a somepath> prompt in Command Prompt to see what we are talking about.

I have indeed not because that’s not covered in the tutorial (yet). I have used Windows Command prompt in other contexts but I didn’t think it was used much anymore.

Thank you for that link. I will read that after I finish the tutorial.