I wrote some very simple code:
def yo():
text = "hi there"
print(text)
print(text)
yo()
I ran this in Spyder and online compilers without error. Obviously it spits out:
hi there
hi there
But when I run this in VS Code terminal using the "Run Python file in terminal" play button I get
"SyntaxError: invalid syntax"
for line 1 (the def line).
When I type yo() into the terminal itself, I get the expected output of:
hi there
hi there
Why is do I get a different result from these? I executed other simple bits of Python in VS Code using the "play" button without issue. It goes without saying that I have the python extension and interpreter installed.
UPDATE: I restarted VS Code and now the file runs without issue. I guess "did you restart the computer" really does solve the issue sometimes...
Your function - yo(), is being defined, however Visual Studio Code does not know how to run it. To fix this, try adding the if __name__ == '__main__': clause. Here is your full code:
def yo():
text = "hi there"
print(text)
print(text)
if __name__ == '__main__':
yo()
Here is some more information about if __name__ == '__main__':
If that doesn't fix it, you must have some formatting issues or some different settings of Visual Studio Code. You could do the following things.
Make sure you're running the right file
Delete all of the code and paste it in again
Reset your Visual Studio Code settings
Make sure your settings for Tab are 4 spaces.
Disable terminal.integrated.inheritEnv in Settings
If all else fails, try these:
You should use the exit() command in the terminal to end python session. Then re-run and see if anything works.
Run your code using 'Start without debugging'.
Related
import pyautogui
while True:
test = pyautogui.locateCenterOnScreen('test.png',region=[600,570,680,570],grayscale=False,confidence=.6)
if test:
pyautogui.moveTo(1000,1000)
print(str(test),'found',test.x,test.y)
break
The print statement is getting run(so its not that the if statement is not getting accessed)
Also the move to does work, if run outside of the while statement
With further inspection/test, visual studio code has to be run as admin in order for (moveto) to work while it's not a focused application. This said, if you are using (moveto) and visual studio code has focus, (moveto) will work.
In conclusion, run VS CODE as admin for (moveto) to work correctly.
print('==', end='', flush=True)
the above code give the error "SyntaxError: invalid syntax" even though it was working fine? All the variables seem to have turned white rather than blue (which is what it used to be) in Visual Studio Code
Help please?!
Here is the whole source code (it's for a hangman game): https://www.codepile.net/pile/QmJE5BYO
I have tried to run your code, except for needing to add alreadyGuessedLetters = [], it works well.
It has not given me the error message of SyntaxError: invalid syntax. So could you provide the traceback of it? Such as a screenshot. And have you tried to restart the VSCode again?
I can't see the color of your code, Have you tried to switch the color theme?
You can take the command of Developer:Inspect Editor Tokens and Scopes to check the colorization ruler of the semantic token like this:
I'm having a problem with unittest testing, code and results below.
I try to run my test from the PythonAnywhere IDE, and it says that 0 test has been made.
I tested the code with prints to find out where things went wrong and I found out that interpreter doesn't even go into the function to see the test.
I know the test names should start with "test_", which they do.
Any other idea?
I am working on pythonAnywhere if it's matter somehow.
My code:
import unittest
import signUpForm
class Test_signUp(unittest.TestCase):
print ("i got to here")
def test_Utility(self):
print ("but never here")
# test all utlity functions in the sign up form. and delete the changes afterward
#check system addition and manipulation
self.assertEqual(addSystem ("dungeons" , 8000) , "added dungeons to systems list")
if __name__ == '__main__':
unittest.main(exit = False)
When I run this I get:
i got to here
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
>>>
That looks like a bug in the IDE in PythonAnywhere -- the easiest workaround would be to close the console in the editor (using exit()), then refresh the page, and start a new console using the "Bash console here" button that will appear where the old console was. Then you can use the command that #jfaccioni suggested in the comments to your question:
python3 -m unittest code.py
...but have your test results on the same browser tab as your editor.
I have registered with exercism.io on the Python track, and haven't got off to a good start! The first exercise is a simple print hello world example, and I am of course able to write the code that executes this. The problem I have is where on earth do I place my code? Should I overwrite the existing hello_world.py file with my own file, or add my script lines to the existing file? I have read the documentation and must be missing something as I can't fathom out what to do with my code to test and submit.
When I download the test material, there is a default hello.world.py file created in the relevant directory, which contains this;
def hello():
pass
There is also a hello_world_test.py that contains this;
import unittest
import hello_world
# Tests adapted from `problem-specifications//canonical-data.json` # v1.1.0
class HelloWorldTest(unittest.TestCase):
def test_hello(self):
self.assertEqual(hello_world.hello(), 'Hello, World!')
if __name__ == '__main__':
unittest.main()
I have written a file called exercism_hello_world.py which contains this;
# This script prints "Hello, World!" to the console
print ("Hello, World!")
# end of script
Can anyone who may already be using exercism.io please advise how / where I place my code so that I can test / submit the first exercise and continue with the learning. Thanks.
After installing the cli script.
Enter the file location of full python file along with the name of the file.
Example
exercism submit C:\Users\srag\Exercism\python\hello-world\hello_world.py
You want the Exercism directory to begin with a capital "E" if you're running on macOS.
Try out:
exercism submit /Users/(your username)/Exercism/python/hello-world/hello_world.py
You should add your solution to the hello_world.py file.
However, you can change the default Exercism workspace directory. If you are on MacOS or Unix you can do that via terminal:
exercism configure --workspace="YOUR_PATH"
By changing this setting, everytime you run the command to "clone" the problem, it will get copied to the path you specified.
Regarding this problem, I am not sure if it helps, but it was stated to return "Hello, World!" and not print it.
I'm just getting started with Python and trying to get some easy code-examples to compile. I am using the 'Spyder' Editor and everytime I run code it shows 'runfile(...)' before the actual compiled code in the console.
Is there a way to prevent this behaviour?
Try including this instead immediately prior to your code. The terminal will now return a clean code only response:
cls = lambda: print("\033[2J\033[;H", end='')
cls()
you are trying to run the code, instead go to settings, keyboard shortcuts, and look for "run selection" it will have a shortcut assigned to it
Now select all the code and use the shortcut
it will only give you in and out