Sunday, April 1, 2018

How do you indent in Python 3.6.2?

Python indenting is tricky...

Python is one of the languages that is very particular about indenting. If the indenting is not correct, all you will get is this,

IndentationError: expected an indented block

After much testing this is what I found will work on Windows 10 command processor. I am using Python 3.6.2 that comes with Anaconda download.

Invoke python -t  from C:\prompt.
I am testing this code:

               for i in range(10)
               print (i)


The code is pretty simple and will work as long as indentation is correct.

>>> for i in range(10):press Enter
...    print(i) between last dot and print(I), insert three spaces  and hit Enter.
In the next line there will be three dots and the (blinking) cursor is one space after the last dot.

Press enter and you see the output
...
0
1
2
3
4
5
6
7
8
9


>>>
Here is another example using the if/else statement



>>> if (a in list):-------     Line1
...    print("a in list")-     Line 2
... else: ----------------     Line 3
...    print("a not in list")--Line 4
...
a not in list

--
Follow this procedure:

Line 1: Type line 1 and enter
Line 2: count 3 spaces after the three dots and type print()etc. and Enter
Line 3: type else: and enter
Line 4: count three spaces after the three dots and type print() etc.
Press enter.
Press enter


No comments: