On python 3.6 console, how do you entered these loop commands? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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.

28th Oct 2017, 5:12 PM
Letlhogonolo Theodore Obonye
Letlhogonolo Theodore Obonye - avatar
2 Answers
+ 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
29th Oct 2017, 3:15 AM
visph
visph - avatar
0
thank you visph. very comprehensive. but I figured it out on my own. I managed to make a few procedures.
29th Oct 2017, 8:39 AM
Letlhogonolo Theodore Obonye
Letlhogonolo Theodore Obonye - avatar