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.
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?
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.
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 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.
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
for x in range(5):
print("final x =", x)
in some file “foo.py”, and another window where I run it like this:
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.
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.
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.
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.