>&g"/> >&g">

Home  >  Article  >  Backend Development  >  How to solve File "<stdin>", line 1 non-grammatical error in Python

How to solve File "<stdin>", line 1 non-grammatical error in Python

WBOY
WBOYforward
2023-04-23 10:58:062410browse

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

Supplement: python {File "", line 1} error

How to solve File <stdin>, line 1 non-grammatical error in Python

##When learning Python, The first program hello.py (as shown below)

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.

How to solve File <stdin>, line 1 non-grammatical error in Python

Because I also entered the pyhon command before executing python hello.py, the correct approach It should be executed directly python hello.py

How to solve File <stdin>, line 1 non-grammatical error in Python

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete