How can I skip the iteration if warning is raised
Suppose I have the code below
import warnings
# The function that might raise a warning
def my_func(x):
if x % 2 != 0:
warnings.warn("This is a warning")
return "Problem"
else:
return "No Problem"
for i in range(10):
try:
# code that may raise a warning
k = my_func(i)
except Warning:
# skip to the next iteration if a warning is raised
continue
# rest of the code
print(i, " : ",k) # Only print this if warning was not raised in try:except
I would expect this to print only even numbers as my_funct(i) will raise a warning for odd numbers
update:
my_func(i)
was used just for illustration purposes, the actual problem I want to use might not have an obvious returned value that raises a warning.