def greet(name):
"""
This function greets to
the person passed in as
a parameter
"""
print("Hello, " + name + ". Good morning!")
This is a basic example for defining a function, and when I run this code on Python 3.8.5 by VSC, the result is like below.
>>> def greet(name):
...
File "<stdin>", line 2
^
IndentationError: expected an indented block
It says that there is an indentation error but I really don't know why. And also, line 2 is not just blank but according to the result, line 2 seems like blank.
for number in range(5):
print("Thank you")
>>> for number in range(5):
...
File "<stdin>", line 2
^
IndentationError: expected an indented block
Same error for 'for'. Can anyone help?
Right now, you are running the Python script line-by-line by placing the VS Code cursor on each line of code and then pressing shift+enter. This utilizes the Python Interactive window afaik. It works fine for running individual lines but if you want to run a collection of lines that include indentation e.g. a for loop or function definition, then select all of those lines together and then press shift+enter. You would need to do this for code that's indented, for example:
def greet(name):
print("Hello, " + name + ". Good morning!")
Alternatively, if you have read Getting Started with Python in VS Code and specifically installed the Python extension for VS Code then you will be able to click the Run Python File in Terminal button (it looks like a Play icon) to run your Python script.
Alternatively, you can launch a Terminal Window in VS Code and manually run the Python script using python3 hello.py (or potentially python hello.py, depending on your Python installation).
You should not press Shift+Enter to run the code in the terminal line by line.
Because the REPL will add an Enter to start a new line automatically like this:
You can select all of the codes then send them to the REPL:
Related
I am a newbie trying to use Python (2.17.15 via Anaconda) on Visual Stodio Code on my Mac. I have the following simple code:
def function(x):
y = x + 2
return y
This code is giving me the usual trouble, an indentation error:
return y
^
IndentationError: unexpected indent
>>> return y
File "<stdin>", line 1
return y
^
IndentationError: unexpected indent
>>>
Needless to say that Jupyter or Spyder have no problem with this. I checked that on VSC tab gives 4 spaces. All similar questions are related to this, but I cannot fix it.
Other, built in functions of Python work fine.
Please give me some help or tips since I do not know how to escape this.
UPDATE
Installing again Python3 this simple code DOES work on Sublime but still not on VS Code. I still get the same error in VS Code.
UPDATE2
So, another update. If I change from return to print and instead of shift-command debug and run the code then it works.
Any idea what is going on?
This looks like it's because you're running the code with Shift+ENTER.
VS Code has the following 2 bindings for Shift_ENTER:
I believe that you're seeing the 2nd of these, which says "Run Selection/Line in Python Terminal. I suspect you have the focus on the return y line, and so it's only running that single line of code.
If, instead of Shift+ENTER, you use the Run Code command in VS Code, you should see it work fine:
You might well think "OK...so if I select all of the code this will work, right?" and I agree...this feels like it should work. However, I see a similar issue. I'll see if I can work out why, but for the moment you can use the Run Code command in VS Code and that will do what you want. If you highlight the code you want to run, that will limit what gets executed.
Run Code can be executed with Ctrl+Alt+N
It looks like this issue (that selected code doesn't run correctly with Shift+ENTER) is a bug that's being tracked by here: https://github.com/Microsoft/vscode-python/issues/2837
And a work around (not ideal) is to add code before/after your function that is NOT indented, and then select and execute those lines too:
print("this...")
def function(x):
y = x + 2
return y
print("...now works if you select all these lines and Shift+ENTER!")
This is a bug from the python extension, which you need to run chunks of code in the interactive mode.
So in the example code below:
for lastRun in list(d_RunPanelsPresent.keys()):
# some indented commands
logFile = f"/nexusb/Novaseq/{lastRun}/logPPscript.txt"
if not os.path.isfile(logFile):
with open(logFile, 'w+') as f:
pass
else:
pass
If I highlight as follows (note where the cursor is):
I will get an error.
The solution is to highlight the code from the very left of the code editor, as shown below:
This works for me 100% of the time.
Forgetting a semicolon at the end of the function definition produces the same error.
Hello everyone I was working on a exercise and also new to the syntax of the use of python. I tried to write this code to show the Max Number:
def maxNum(a,c):
if a>c:
return a
else:
return c
print(maxNum(16,20))
a little background when I use maxNum(16,20) or print(maxNum(16,20)) I get a SyntaxError: invalid syntax in the interactive shell however when I use a new window and run the above script the answer 20 is shown. Why is it the above script has to be ran from a new window and not in the shell it to work? In addition is there a website that shows when or how to indent? Thanks
You need to validate the function definition by pressing [ENTER] and then put the rest of code
def maxNum(a,c):
if a>c:
return a
else:
return c
print(maxNum(16,20))
You have extra spaces on the apparently empty line between the end of the function definition and the print call. With that, the interpreter is expecting more content in the function. To end a compound statement in an interactive session, you need two linefeeds in a row.
I am a newbie trying to use Python (2.17.15 via Anaconda) on Visual Stodio Code on my Mac. I have the following simple code:
def function(x):
y = x + 2
return y
This code is giving me the usual trouble, an indentation error:
return y
^
IndentationError: unexpected indent
>>> return y
File "<stdin>", line 1
return y
^
IndentationError: unexpected indent
>>>
Needless to say that Jupyter or Spyder have no problem with this. I checked that on VSC tab gives 4 spaces. All similar questions are related to this, but I cannot fix it.
Other, built in functions of Python work fine.
Please give me some help or tips since I do not know how to escape this.
UPDATE
Installing again Python3 this simple code DOES work on Sublime but still not on VS Code. I still get the same error in VS Code.
UPDATE2
So, another update. If I change from return to print and instead of shift-command debug and run the code then it works.
Any idea what is going on?
This looks like it's because you're running the code with Shift+ENTER.
VS Code has the following 2 bindings for Shift_ENTER:
I believe that you're seeing the 2nd of these, which says "Run Selection/Line in Python Terminal. I suspect you have the focus on the return y line, and so it's only running that single line of code.
If, instead of Shift+ENTER, you use the Run Code command in VS Code, you should see it work fine:
You might well think "OK...so if I select all of the code this will work, right?" and I agree...this feels like it should work. However, I see a similar issue. I'll see if I can work out why, but for the moment you can use the Run Code command in VS Code and that will do what you want. If you highlight the code you want to run, that will limit what gets executed.
Run Code can be executed with Ctrl+Alt+N
It looks like this issue (that selected code doesn't run correctly with Shift+ENTER) is a bug that's being tracked by here: https://github.com/Microsoft/vscode-python/issues/2837
And a work around (not ideal) is to add code before/after your function that is NOT indented, and then select and execute those lines too:
print("this...")
def function(x):
y = x + 2
return y
print("...now works if you select all these lines and Shift+ENTER!")
This is a bug from the python extension, which you need to run chunks of code in the interactive mode.
So in the example code below:
for lastRun in list(d_RunPanelsPresent.keys()):
# some indented commands
logFile = f"/nexusb/Novaseq/{lastRun}/logPPscript.txt"
if not os.path.isfile(logFile):
with open(logFile, 'w+') as f:
pass
else:
pass
If I highlight as follows (note where the cursor is):
I will get an error.
The solution is to highlight the code from the very left of the code editor, as shown below:
This works for me 100% of the time.
Forgetting a semicolon at the end of the function definition produces the same error.
On 31:43 of Python 3 Metaprogramming youtube video David Beazley advances to a new line of Python IDLE (31:43) without forcing an execution. What keyboard key or combinations of keys is used to make it work?
If you open an indent block in the IDLE, hitting Enter will not execute the line.
>>> class A(Base):
...
Hitting Enter after an empty line will execute the whole block of code.
>>> class A(Base):
... pass
...
>>>
He's not typing anything special; Idle doesn't end the class definition (started by the class keyword) until you enter a blank line.
In the video you linked, David is not using Python IDLE (Integrated Development Learning Environment), which is an instance of an IDE, he is using the Python Interpreter which is a binary executable for the interactive Python shell for executing python statements.
In all of these interactive interpreters, when you have an expected indent, usually in loops, conditions and in your case a class, the line ending in a colon : will not execute when you hit the Enter key it will allow you to keep typing in the next line until you hit Enter twice.
I'm trying to debug a script inserting print() commands to see the states of variables. But those commands don't seem to work. When I debug line by line using PyScripter it just skips those lines. I'm using Python 3. The problem is in a method of a class defined in a .py file, called from another .py file. I insert part of the code below.
class BaseGen:
def duracion(self, rangos, anio, semanas, verificar_ver_py=True):
print('some string')
The calling line is:
duracion = base.duracion(rangos_parte, anio, semanas)
The problem was with PyScripter. I tried the same code on the Python console and it was just fine.