0
This lesson was confusing me
Hello, I'm still confused about this Python course. Why does the lesson called "debugging" state that lines 2 and 3 are bugs? Meanwhile, learning "exception" states that execution will stop at line 3. Can you please explain? Code: salary = 50000 role = Analyst age -> 29 print(salary) Thank you
6 Answers
+ 2
In the given code:
1. `role = Analyst` is missing quotation marks around "Analyst", making it a variable name instead of a string.
2. `age -> 29` should be `age = 29` to assign the value 29 to the variable age.
In the "debugging" lesson, these issues might be pointed out as potential bugs because they would cause errors when the code is executed.
In the "exception" lesson, if the error in line 2 is not caught and handled properly, the execution will indeed stop at line 3 because of the error caused by the incorrect syntax.
So, both lessons highlight different aspects: "debugging" focuses on identifying and fixing bugs, while "exception" discusses how errors can lead to program termination if not handled appropriately.
+ 1
I'm Rikka ,
Sololearn forgot to teach us about how the parser and its exceptions behave relative to other exceptions before quizzing us on them.
I researched it on python.org and learned what I believe is really going on (refinements are welcome).
The parser checks the source code before the program runs.
The parser is responsible for raising any of these three exceptions.
SyntaxError
IndentationError
TabError
If the parser raises one of those exceptions, it stops, and the compiler and interpreter never get a chance to run, meaning that any potential run-time exceptions that might have been raised by the interpreter never get a chance to occur.
Therefore, the SyntaxError on line 3 is found and the potential NameError on line 2 is not, even though line 2 is earlier in the code than line 3.
If you were to correct the SyntaxError and run it again, the NameError would be raised.
+ 1
It is because Python is an interpreted language, while C# and Java are compiled language.
The Python interpreter read the code on the fly. Therefore, it will throw an exception when an error occurred and stop reading the next line of code.
And this code shows the behaviour.
In a compiled language, the compiler checks for the syntax error before compiling the code. Which means it read all the codes before compiling, so you can read every exception the compiler found.
https://sololearn.com/compiler-playground/cGwzUliUq5YX/?ref=app
+ 1
I'm Rikka ,
Yes. An exception raised by the parser takes priority over any run-time exceptions, because the parser examines the code first and can prevent the code from being run by the interpreter.
+ 1
In the code snippet you provided, the variable `role` should be a string, but it is not enclosed in quotes. It should be `role = 'Analyst'` to be treated as a string. Without quotes, Python will consider `Analyst` as a variable, which is undefined in this context.
The third line `age -> 29` contains invalid syntax. You should use the assignment operator `=` instead of `->` for assigning a value to a variable. So, it should be `age = 29`.
Considering these corrections, here is the updated code snippet:
salary = 50000
role = 'Analyst' # Enclose 'Analyst' in quotes to make it a string
age = 29 # Use "=" for assignment instead of "->"
print(salary)
Debugging typically involves identifying and fixing errors in the code. In this case, before fixing the issues as mentioned above, lines 2 and 3 were considered as "bugs" because they contained syntax errors and incorrect variable assignments.
0
Ohh, I see... So the exception raised by the parser must take priority? Why isn't it like C# or Java, where every exception or error is displayed on a new line?