I am relatively new to Python and would like to know if there is a way to run an "in-elif-else" construct interactively line-by-line (as can be done similarly for an "if-else if-else" construct used in the R language) from the Python Editor in Visual Studio Code and have the output displayed in the terminal. I downloaded the geddski vscode extension which allowed me to create a macro that runs a line of code and moves the cursor down to the next line, and which also displays the output in the terminal. This is how I set that up in my settings.json file:
"macros":{
"pythonExecSelectionAndCursorDown": [
"python.execSelectionInTerminal",
"cursorDown"
]
},
I then mapped this macro to the keyboard shortcut "Ctrl + Enter", as can be seen in my keybindings.json file:
{
"key": "ctrl+enter",
"command": "macros.pythonExecSelectionAndCursorDown",
"when": "editorTextFocus && !findInputFocussed && !python.datascience.ownsSelection && !replaceInputFocussed && editorLangId == 'python'"
},
This works well when I am running certain lines of code, especially when assigning values to variables. However, this doesn't work when I try to run an "if-elif-else" construct. In the sample file below, I run the first line of code to assign -2 to x without a problem. But I get an "IndentationError: expected an indented block" error message when I attempt to run line 4 after running line 3.
x = -2
if x < 0:
x = 0
print("Negative changed to zero")
elif x == 0:
print("Zero")
elif x == 1:
print("Single")
else:
print("More")
This is how the error appears in the terminal:
>>> if x < 0:
... x = 0
File "<stdin>", line 2
x = 0
^
IndentationError: expected an indented block
>>>
I know what the Indentation error means; however, I am not sure how to fix it in this scenario where I am trying to run code interactively. Can somebody point me in the right direction, or suggest a different a different way to accomplish what I am trying to do? Like I mentioned I am new to Python and would like to know what the best practice would be in this case. Thanks for reading this.