Hi there,
If there is an error in the Python code - why does the whole program shut down? and notify of a possible error?
In the example here there are two print() commands. If one is valid but the other is not the console only displays an error and does not ‘print’ the correct result.
Thanks in advance.
If something goes wrong in some sequence of steps that rely on each other, then there’s no meaningful way to continue, you want that to stop.
If it’s a bug then fix the bug.
If something was wrong about the outside world, then you might ignore the request that causes something to go wrong, and continue with the next one. Example: permission denied when opening a file -> don’t open that file, continue with next file, assuming that they are independent units of work and that it makes some kind of sense to continue.
I agree like in life if a machine a part of it is spiolt the rest will work unless it is a important part of the machine speaking as someone who understands basic syntax
Would there be any instances when you could bypass an error to keep the program running? If so, how and why?
Example code:
Parenthesis within parenthesis
print(“Then there he stood, surveying the crop fields before saying “We have an infestation.””)
print(‘line 1 \n\nline 3’)
Plus-Equal Operator
counter = 0
counter += 10
print(counter)
This is equivalent to
counter = 0
counter = counter + 10
print(counter)
The operator will also perform string concatenation
message = "Part 1 of message "
message += “Part 2 of message”
print(message)
Python ZeroDivisionError
numerator = 100
denominator = 0
bad_results = numerator / denominator
Modulo operations
zero = 8 % 4
print(zero)
nonzero = 12 % 5
print(nonzero)
String concatenation
first = "Hello "
second = “World”
result = first + second
print(result)
long_result = first + second + “!”
print(long_result)
print(“Hello World!”)
print(100)
pi = 3.14159
print(pi)
message = "The pressure is "
valve_pressure = 30
print(message + str(valve_pressure))
Greeting
message_string = “Hello there”
print(message_string)
Farewell
message_string = “Hasta la vista”
print(message_string)
When I assign two different values to the same variable, why there is no error, but Python types the second one?
name = “Mark”
name = “Sue”
print(name)
And the result is: Sue
Thanks
I believe it assumes you’ve (intentionally) updated the value of that variable, therefore it doesn’t handle this as a mistake. Python will use the latest value of the variable, in this case “Sue”.
Great post, @brownwb
I didn’t see an answer, but I’m similarly curious as well. Error handling may be covered later, but I’m hoping Python has that built in somewhere.
There certainly is, if you’re just looking for the basics then the following should give you a nice quick intro (whole page is useful in general if you’re unfamiliar with it): 8. Errors and Exceptions — Python 3.9.5 documentation
If that’s not enough explanation/examples then have a wee web search for the same tools and you should be able to find plenty of guidance.
Hi! I think the reason it does not print the correct response is because the underlying Python program is not designed to give you suggested corrections, but instead immediately identify sources of error and let the programmer(s) figure it out for themselves. It’s this way because programming should remain unbiased. Being able to better understand the error not only keeps the code unbiased, but it’s better for the programmer’s own edification as well.
In my experience, sometimes, the error doesn’t happen at ^
, it places somewhere near it. So it is not good for newbie like me
Another thing, if Python doesn’t stop when matching an issue, then it can lead the whole code wrong. But, why not? it these codes like a module: they are independent, like docker container
way.
This might also have to do with the fact that Python is an interpreted language, which means that execution of the code occurs line by line. So when the error is approached, execution stops to allow handling
Question 2: Why does an error in Python shut done the whole program?
Answer:
In a Python program, each statement must be read and interpreted before progressing to the next statement. If no interruption affects the progression of reading the program, it will progress to completion. However, ‘errors’ impede the progression of the program from completing the process. Once it is debugged, the Python interpreter can progress to the end of the program or coding.