-4

I am using Python

but the space gap is making my life very hard with it

example

when I use the if statement

if Parm2 == 1:
    Ch = "A"
elif Parm2 == 2:
    Ch = "B"
elif Parm2 == 3:
    Ch = "C"
else:
    continue
mdl = CallFunc(Parm2)

print("XX Always Print XX")

now the "XX Always Print XX" should be printed regardless

but due to my mistake it is inside the if statement which cause me long time to find

the actual if statement is nested and longer

I wonder if there is a method I can use begin/end or {} in such statements in Python

something like

UPDATE

for the people who focus on the IF statement

if Parm2 == 1:
{
    Ch = "A"
}
elif Parm2 == 2:
{
    Ch = "B"
}
elif Parm2 == 3:
{
    Ch = "C"
}
else:
{
    mdl = CallFunc(Parm2)
}
print("XX Always Print XX")

Happy now??

ok now how to get the brackets work in Python?

asmgx
  • 7,328
  • 15
  • 82
  • 143
  • 2
    No, Python only uses indentation. – Barmar Dec 31 '21 at 04:17
  • 3
    the braces in your example aren't even correct. There should be braces around each `if` and `elif` body, not just around the whole thing. – Barmar Dec 31 '21 at 04:18
  • 1
    your 'something like' example make no sense.. – All Іѕ Vаиітy Dec 31 '21 at 04:18
  • 2
    Even when you use braces, you should always indent it properly so that the structure is more easily visible to everyone. – Barmar Dec 31 '21 at 04:20
  • 1
    `else: continue` makes no sense here. `continue` can only be used in loops. – Barmar Dec 31 '21 at 04:20
  • Why do you have `mdl = CallFunc(Parm2)` inside the braces? It should also be done regardless. – Barmar Dec 31 '21 at 04:22
  • It's not even possible to have the `print` inside the `if`, even if you screw up your indentation, and if `mdl = ...` is outside the `else`, then it's not even possible to have the `print` inside the `else` either. – user2357112 Dec 31 '21 at 04:27
  • Explicit block delimiters are *way* easier to screw up than indentation. You can see how indented a line is by looking at the line. You can't see how many braces deep a line is without reading the whole file up to that point. – user2357112 Dec 31 '21 at 04:28
  • @Barmar i dont know why you attacking me assuming my code is wrong.. this code is part of a bigger code as I mentioned this if statement is inside a loop and the brackets I used just to give an example of what I want.. and I want the mdl = Func inside the if and print outside the if – asmgx Dec 31 '21 at 04:31
  • "and I want the mdl = Func inside the if and print outside the if" - that makes no sense. It indicates you don't even know how `if` works. You need to get the basics down first. – user2357112 Dec 31 '21 at 04:36
  • It took me a while to get used to after switching from JS, but after a while you just don't think about it. From your bracketed example it is quite hard to see when you want `mdl = CallFunc(Parm2)` to be executed though. – Christy Kail Dec 31 '21 at 04:38
  • It doesn't seem like braces would help you, since the braces used in your "something like" code make no sense. – khelwood Dec 31 '21 at 04:38
  • 2
    @asmgx: You've got four people here all telling you your braces make no sense, and no one saying "yeah, that looks right". You should reconsider your position. – user2357112 Dec 31 '21 at 04:42

4 Answers4

3

Python is indentation based. Yeah, its harder to read and easier to make mistakes like you indicated, but that's what it is.

SledgeHammer
  • 7,338
  • 6
  • 41
  • 86
2

Think about downloading an IDE for python, like Pycharm, they are helpful for identifying errors like this one, they also have an "auto-indent" feature. But no, Python is indentation based.

1

What the Python designers realized is that braces or begin/end keywords are mostly noise to human programmers. They generally recognize the structure of code by its layout. For instance, if you were to write the C code:

if (condition)
    x = y;
    w = z;

a human would often not notice that the braces are missing, and assume that both assignments are controlled by the condition. Writing code like this is a common error, especially when you start with a block that has just one statement (so the braces are optional and were omitted), and forget to add braces when a second statement is added. (See Why is it considered a bad practice to omit curly braces?).

Conversely, if you write

if (condition) {
    x = y;
w = z;
}

it looks like w = z; is not part of the conditional.

Braces mainly exist for the benefit of software that processes code (compilers, editors, IDEs), they make it easier for them to detect groups of code. The Python designers decided to mirror the way humans read code in their parser, rather than forcing humans to adapt to the computer's needs.

Braces allow for more flexible code layout, but in practice it's usually condiered wrong to take advantage of this. Writing code like

while (something) { statement1; statement2; 
statement3; }

is less readable than

while something:
    statement1
    statement2
    statement3

Python does allow some flexibility: You can separate statements on the same line with ;, and put the contents of a conditional on the same line after the :. But writing like this is not considered Pythonic, and should be used only in very special circumstances (this blog post describes those cases).

There's always some adjustment necessary when you're learning a new programming language and you're accustomed to the patterns of the languages you previously used (many programmers have refused to learn Lisp, because of its Lots of Irritating, Stupid Parentheses). But give it a little time and you'll get used to it.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • the problem is that when you have nested and multiple if/while statements these indents will become less visible and hard to tell which is which and easy to make mistakes.. add to that the problem in different indentation chars (linux, windows, IDE) i had to spend days change between TAB and double space and 4 spaces and many errors if change code from one platform to other.. the format of the script can be done easily with { } – asmgx Oct 29 '22 at 04:14
  • The best advice about deeply nested code is **don't** do it. Break your code into functions. – Barmar Oct 29 '22 at 04:21
0

This shows how you can hack it:

if True: {
    print("hello")
}

If you do a google search for "python what if you don't want to use indentation for blocks" you might get: peach pit python indentation

Programmers familiar with other languages often bristle at the thought that indentation matters: Many programmers like the freedom to format their code how they please. However, Python indentation rules are quite simple, and most programmers already use indentation to make their code readable. Python simply takes this idea one step further and gives meaning to the indentation.

i.e. they force you not to use indentation.

My problem with this is that I love to use emacs auto-indentation to reindent the whole code file but this totally screws up the indentation in python; in C or C++ this finds the indentation problems and makes them evident; in python it loses all your information and changes the meaning of the program;

Don't get me wrong I want to use BOTH rigorous indentation AND curly braces;

You can use the hack above to "circumvent" python indentation but when writing code for anyone other than yourself it won't be popular.