0

Why aren't the first two lines of this code executed in pycharm?

print('Hi')
print('How are you?')
while

I know there is a problem with the third line but considering the fact that Python is an interpreted language, shouldn't the first two lines be executed before an error is detected? As I know interpreters process the code line by line and stop at the point where an error occurs.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • 4
    *"As I know interpreters process the code line by line and stop at the point where an error occures."* This isn't literally true. See: https://stackoverflow.com/questions/6889747/is-python-interpreted-or-compiled-or-both Some errors will only be observable at runtime, but others (like yours) will prevent the source code from being compiled to bytecode, so that nothing gets executed. – slothrop Jul 16 '23 at 14:53
  • 1
    It's worth noting that the "interpreted vs compiled language" is debate is almost always a false dichotomy these days since virtually no truly "interpreted" languages exist, aside from shell languages. Nowadays, "interpreted" tends to just mean "compiled quickly" – Brian61354270 Jul 16 '23 at 15:09
  • 1
    Related: [Why Python compiler doesn't ignore syntax errors after exit()?](/q/73129145/4518341) – wjandrea Jul 16 '23 at 15:10
  • BTW, welcome to Stack Overflow! Check out the [tour], and [How to ask a good question](/help/how-to-ask) if you want tips. – wjandrea Jul 16 '23 at 15:14
  • Pycharm has nothing to do with it. You'd get the same error any way you run your code as a script. In a REPL it's different though. – wjandrea Jul 16 '23 at 15:19
  • slothrop Thank you for the useful reference you gave. Can you please give me a brief explanation on what a bytecode is and also how interpreting is different from compiling? why don't we say the bytecode will be compiled instead of saying it will be interpreted? I'd be very grateful. – user1163451 Jul 16 '23 at 15:38
  • Brian61354270 Thanks a lot for letting me know that. – user1163451 Jul 16 '23 at 15:40
  • bytecode is a simplified language with a limited number of instructions. You can see an example here (and a good discussion of "interpreted" vs "compiled"): https://nedbatchelder.com/blog/201803/is_python_interpreted_or_compiled_yes.html – slothrop Jul 16 '23 at 16:15
  • slothrop Thank you so much for your help. The passage was indeed informative. – user1163451 Jul 16 '23 at 17:07

2 Answers2

0

I'm not sure (this is how Java works) but the compiler checks the file for any errors, and if there are none, it runs. That may be your issue.

I'm a Java developer, so I don't know much about Python.

My main question is: does your code run properly without the error?

0

Syntax error on line 3.

When you´re trying to execute that file it will be compiled. If there are any syntax errors it will not compile and therefore will not run the code. Not even the first two lines.

Remove line 3 or make a valid loop, like:

while True: 
    print('endless loop!')
aurx
  • 48
  • 5