Written a very simple program in VSCode:
str1 = 'Python is good' print(str1.replace('Python', 'python'))
When I click to run it in the terminal, an error message appears:
File “”, line 1
& C:Python challenge /test.py"
^
SyntaxError: invalid syntax
is not a syntax problem, but because the program is run under the command line of the Python interpreter:
>>> & C:/Python challenge/test.py"
It should be run under the CMD of Windows, so exit the Python interpreter first:
>>> ^Z
Click to run in the terminal and it will be OK:
PS C:Python challenge/test.py"
python is good
print("hello welcome to python world")The above error is reported when running, because the command line instructions have been The Python interpreter has been run. Pay attention to distinguishing between the command line environment and the Python interactive environment. As shown in the figure below, enter
python directly to enter the interactive mode. When >>> appears, you have entered the Python interactive environment, which is equivalent to The Python interpreter is started, waiting for you to enter the source code line by line, and execute each line entered. Now that the .py file has been written, if you want to execute the entire source code at once, you should run
directly. .py file instead of entering the source code interactively.
The above is the detailed content of How to solve File '<stdin>', line 1 non-grammatical error in Python. For more information, please follow other related articles on the PHP Chinese website!