'Updating global variables from try block
I would like to update the global variable status after the try block runs successful. Is there a way to do so. The code below seem not work as intended.
status = "unsuccessful"
def test_func(a, b):
global status
try:
return a + b
status = "success"
except TypeError:
print("Wrong")
print(test_func(20, 1))
print(status)
Solution 1:[1]
The issue you are facing here is because status = "success" will never be reached. If your code enters the try block it'll return a+b and exit the block. Putting the status = "success" line above the return line will fix your issue.
try:
status = "success"
return a + b
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Amr |
