0
On python 3.6 console, how do you entered these loop commands?
each time I press 'enter' the console takes it as a new line of code separate from the previous. so my while loop is taken as an error in coding.
2 Antworten
+ 4
A loop is expected to be write with indentation:
for i in range(4):
print(i) # space at begin of line is indentation and mark the block of code to be looped
In the CLI (commad line interpreter -- the console), you can only enter one line at a time, so when you press [enter] you could expect that the loop delaration will run without having set its body block of instruction... but to prevent this behaviour and let user input commands of loop line by line, the Python CLI use this rule:
+ after a line requiring a block of (indented) instruction, the code is NOT executed immediatly, and Python interpreter wait for a validated empty line to confirm the end of the (indented) block.
So, to enter your multiple lines in the CLI, do it one by one, each validated (but not ran) by pressing [enter] key (wich produce a new line) and start your new lines to be in the sub-block with correct indentation:
>>> for i in range(4):
>>> print(i)
>>>
line 1: # [enter] doesn't run this line because expect for instruction block
line 2: # [enter] still doesn't run this line because wait to block end confirmed by empty line
line 3: # [enter] will now run the bunch of line previously entered because empty line validate end of block
0
thank you visph. very comprehensive. but I figured it out on my own. I managed to make a few procedures.