-1

Why is the CPython interpreter not executing the previous statements if an EOL syntax error occurs in a later line? I thought that an interpreter is supposed to execute all the statements in a file one by one.

print("Hello World")
print("how are you')
# Why is the interpreter not executing the first line?
3
  • 2
    Typo. Ether use single or double quotes for strings, but do not mix them. Could be print("how are you") or print('how are you'). Commented Oct 5, 2024 at 12:10
  • 2
    The question is if there is a typo in the second print statement, why the first print is not executed? The interpreter will execute all statements before the error statement, right? Commented Oct 5, 2024 at 14:22
  • 1
    Possible duplicate: Why is Python able to run preceding code when there is an error in a following line, except a SyntaxError? Commented Oct 5, 2024 at 16:33

2 Answers 2

3

Parsing the file happens before the file is compiled into bytecode for the interpreter to execute. If parsing fails because of a syntax error, the whole process stops there.

Sign up to request clarification or add additional context in comments.

2 Comments

Is it because of Python which is both compiled and interpreted?
Basically yeah. Generally compilation of any programming language starts with lexing (turning text into tokens) and then parsing (making sense of the sequence of tokens), if either fails, the file(s) is not conceptually valid, so there is nothing sensible that could be compiled (to bytecode in CPython's case). E.g., a Bash script is just single-pass interpreted, so lines are executed until a syntax error is hit.
-2

The second line is using an " and a ', you have to pair them instead of print("how are you') you should use print("how are you") or print('how are you')

Edit: I think you wanted an answer explaining why the first one doesn't work if the second one is the problem. So, in a dumbed down explaining: It basically "checks" that there are no syntax errors before actually running the file.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.