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.
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