I want to end a for loop if there is an exception error. And when i use break statement it doesn't end the loop but when i used sys.exit() it ends the loop just ok.
The code i wrote is for calculating ip addresses, but this function below checks if a given ip address have 4 octets and also checks if an octet is an integer value.
This works ok but i don't know if it's efficient to do that. I'm beginner in Python and i wanted to understand the difference between the two.
def check_octet_range(self):
if len(self.ip_address_list) == 4:
for ip_index, address in enumerate(self.ip_address_list, start=1):
try:
address = int(address)
except ValueError:
print(f"Please enter an integer number at octet {ip_index}")
sys.exit()
if address not in self.octet_range:
return ip_index
return self.ip_address_list
else:
print("The IP address range is more or less than 4 octet")
sys.exit()
breakorsys.exit(). E.g., justprint("at end")fully outdented, after you callcheck_octet_range().return(to return from the function) instead of abreak(to exit the loop)?breakbreaks out of loops, whilesys.exit()triggers the interpreter to exit through the documented method. Please consult the documentation and search elsewhere online as fundamental questions like these are often discussed previously elsewhere. If you want to "exit" from anifclause this question is titled that.