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:
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.
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'.
loc = 'Bank'
if loc == 'Auto Shop':
print("Cars are cool!")
elif loc == 'Bank':
print('Money is cool!')
elif loc == 'Store':
print('Welcome to the store!')
else:
print("I do not know much.")
I'm trying to revise some beginner level Python and have been using Visual Studio Code (on Mac) for my scripts. Whenever I try to run blocks of code such as the if statement above, I get error codes like "SyntaxError: invalid syntax" and "IndentationError: unexpected indent". Tried doing my indentation over again and checked my indentation in Sublime and it was fine. The code also runs in my terminal.
I think something is up with my VSC, it's running Python 3.8 which is what I used in my Mac terminal to run the code just fine, but is acting funky when using its terminal.
This is bothering me as it means I might need to switch editors for when I do more complicated stuff later.
Any help would be well appreciated.
If you're sending this line-by-line then you could get into a situation where your code doesn't line up with what the REPL is expecting.
I'll also say you can simplify your code a bit if you want to make it more flexible:
loc = 'Bank'
messages = {"Auto Shop": "Cars are cool!", "Bank": "Money is cool!", "Store": "Welcome to the store!"}
print(messages.get(loc, "I do not know much."))
I am using Pycharm 2018.3.4. When python 3 is used, it indicates error for the end="" in the print function.
However, the code could still be run without any problem. So how to remove the red line for syntax check?
(I feel pycharm gets more popularity in stackoverflow than superuser, so I asked the question here)
Check your Interpreter's version is python 3.x in the right bottom of the pycharm, weather you are 3.x, You could try to retry the "InterpreterSetting" , this buttom.
I am not English speaker, I was left a comment by my language, but it was deleted by someone.
enter image description here
I posted the following question regarding nose and parameterized tests:
use-class-method-in-nose-parameterize.expand call
and I got my answer, but now I wonder why PyCharm is failing to recognize this as valid code. Does anyone know how to turn off this warning in Pycharm, or should i submit this as a bug to jet brains?
Here is what I know works, but PyCharm provides false negative error messages:
class MyUnitTestClass(TestCase):
def generate_scenarios():
yield ('this_is_my_test', 1, 2)
#parameterized.expand(generate_scenarios())
def test_scenario(self, test_name, input, expected_output):
self.assertEquals(input+input, expected_output)
you can suppress anything you want in pycharm
put the cursor right in between the parentheses (where it is underlined in red)
press alt+enter to bring up suggestions
press the right arrow key on the "Add self" line at the top of the suggestions
select the option to suppress error
see also:
https://www.jetbrains.com/pycharm/help/suppressing-inspections.html