1
while 1 == 1:                          line(1)
    x = 1                              line(2)
    print(x)                           line(3)
        x = x + 10                     line(4)

I started using python today and I learned that it doesn't use brackets {} like java to close a loop, but it uses indentation for it.

The code above runs only if I delete line(4). How should I modify the code so it runs with the last line? I used the the formatting from netbeans and still doesn't run.

How indentation works in python? I find it very weird that it doesn't use brackets.

Pinteco
  • 247
  • 1
  • 8
  • 3
    the syntax is not correct, `x = x+10`must be in the same tab as `print(x)` – PV8 Jun 21 '19 at 06:29
  • If your not familiar with indentation you can alwais use ```from __future__ import braces``` at the beginning of your code for some help – Xiidref Jun 21 '19 at 06:51

4 Answers4

2

How indentation works in python? I find it very weird that it doesn't use brackets.

Think like { = indent, } = outdent, but you can't have random blocks "just because" - each such block needs a control structure to introduce it. Thus, your code would be written like this in a brackety language:

while (1 == 1) {
    x = 1
    print(x)
    ???? {
        x = x + 10
    }
}

The block of x = x + 10 does not correspond to any control structure, and thus should not be indented more than print(x).

Amadan
  • 191,408
  • 23
  • 240
  • 301
2

just compare syntax difference below between javascript and python.

javascript:

function foo() {
    for (var i=0; i<10; i++) {
        console.log(i);
    }
}

python

def foo():
    for i in range(10):
        print i

In your case

your code

while 1 == 1:                          
    x = 1                           
    print(x)                           
        x = x + 10  

equivalent javascript code

while(1==1){
var x=1;
console.log(x) {  
     x = x + 10 ;
     }
}

which doesn't make sense. It should be

while(1==1){
var x=1;
console.log(x);
x = x + 10 
}

equivalent python code

while 1 == 1:                          
    x = 1                           
    print(x)                           
    x = x + 10

I just tried to correct your indentation problem but actually above code is invalid if you are looking for increment because x=1 assignment is inside loop which mean print(x) always prints 1

Corrected Code

x = 1 
while 1 == 1:
    print(x)
    x = x + 10
vignesh karnika
  • 111
  • 1
  • 5
  • Yeah, it's ignoring the x = x + 10 and printing just one's. I want to increment my variable x, 10 by 10. – Pinteco Jun 21 '19 at 06:51
1
while 1 == 1:                          line(1)
    x = 1                              line(2)
    print(x)                           line(3)
    x = x + 10                         line(4)

Java equivalent ->

while (1 == 1)                         
{
    x = 1                              
    System.out.println(x)                
    x = x + 10                         
}

Everything indented on the same indentation is treated like brackets in Java. Indentation is very important in Python compared to Java where you can have different lines of codes with different indentation.

Arne
  • 1,293
  • 1
  • 7
  • 20
  • 1
    It's just printing one's. it completes ignores the x = x + 10 – Pinteco Jun 21 '19 at 06:36
  • 1
    @uditkanotra We know, it's just an example. – Pinteco Jun 21 '19 at 06:37
  • @uditkanotra yeah, "while 1 == 1" resolves to true all the time so it'll result in an infinite loop – Arne Jun 21 '19 at 06:44
  • @Pinteco because x is set back to 1 on every iteration, so technically "x = x + 10" is not ignored. if you want to add 10 to x on every iteration, you need to define x outside the loop – Arne Jun 21 '19 at 07:40
1

python doesn't use brackets to declare a scope. it uses tabs instead. you just need to put your statements in the same tab.

correct code is:

while 1 == 1:
    x = 1
    print(x)
    x = x + 10
MohammadHosein
  • 133
  • 1
  • 1
  • 11