3

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()
5
  • Try doing something else after break or sys.exit(). E.g., just print("at end") fully outdented, after you call check_octet_range(). Commented Jun 22, 2020 at 1:31
  • 2
    After reading the relevant documentation, what are some hypothesis on how they may differ? - See sys.exit and "break and continue Statements.." Commented Jun 22, 2020 at 1:33
  • 1
    p.s. both forms will "end the loop" shown in the code. Perhaps the code should use a return (to return from the function) instead of a break (to exit the loop)? Commented Jun 22, 2020 at 1:40
  • 1
    break breaks out of loops, while sys.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 an if clause this question is titled that. Commented Jun 22, 2020 at 1:41
  • What is the similarity? Commented Jun 22, 2020 at 2:11

2 Answers 2

5
  • break is used to exit a loop

  • sys.exit() is used to terminate the running program.

Sign up to request clarification or add additional context in comments.

4 Comments

so in this case sys.exit() is ok to use for this situation?
@KwameBenqazy it is OK as long as you want to terminate the program at that point.
oh Great. But when i tried the break statement in place of the sys.exit() it repeats the first print statement 4 times before breaking out of the loop.
@KwameBenqazy how soon you exit from the loop should not depend on break or sys.exit(), it should be the same for both.
3

A "break" is only allowed in a loop (while or for), and it causes the loop to end but the rest of the program continues.

On the other hand "sys.exit()" aborts the execution of the current program and passes control to the environment.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.