Continue With Loop With Error Python

Python Continue - Controlling for and while Loops

continue is used to skip the remainder of a loop when certain conditions are met. When called, continue will tell Python to skip the rest of the code in a loop and move on to the next iteration.

To demonstrate, let's look at how we could use continue to print out multiples of seven between one and fifty. Notice how the print() statement is skipped when the if statement is true:

            for number in range(1, 51):     if (number % 7) != 0:         # the current number is not a multiple of 7, so continue until the next number         continue      print(f'{number} is a multiple of 7')          

Out:

            7 is a multiple of 7 14 is a multiple of 7 21 is a multiple of 7 28 is a multiple of 7 35 is a multiple of 7 42 is a multiple of 7 49 is a multiple of 7          

if, while and for statements are fundamental in any large Python script (and a few small ones too). These statements follow a stringent set of rules predefined by Python, so we sometimes need to use what are known as control statements to influence them. The three control statements are pass, continue and break, allowing you to govern your code in different manners.

As mentioned previously, continue is used to skip to the end of the current iteration of a loop. Therefore, Python will only bypass code in situations that trigger continue. For a more complex example, let's say we'd like to iterate through a list of numbers and find the square root of each number in the list. For this example, we'll use math.sqrt:

            import math num_list = [49, 25, 36, -9, 4, 64, -25]  for num in num_list:     print(f"The square root of {num} is {math.sqrt(num)}")          

Out:

            The square root of 49 is 7.0 The square root of 25 is 5.0 The square root of 36 is 6.0          

Out:

            --------------------------------------------------------------------------- ValueError                                Traceback (most recent call last) <ipython-input-2-5cd69985842d> in <module>       3        4 for num in num_list: ----> 5     print(f"The square root of {num} is {math.sqrt(num)}")  ValueError: math domain error          

Once our for loop reaches minus nine, our Python script crashes. The reason that our program crashes is math.sqrt doesn't work with negative numbers. One way of avoiding this error is by using continue like so:

            for num in num_list:     if num < 0: # check if the number is negative         print(f"{num} is negative, so it has been skipped")         continue      print(f"The square root of {num} is {math.sqrt(num)}")          

Out:

            The square root of 49 is 7.0 The square root of 25 is 5.0 The square root of 36 is 6.0 -9 is negative, so it has been skipped The square root of 4 is 2.0 The square root of 64 is 8.0 -25 is negative, so it has been skipped          

Adding continue here means Python will skip any negative numbers, preventing us from getting a value error. The diagram below shows the process followed inside of our for loop:

continue is often used to skip error cases in Python, as it's considered more Pythonic than using an exception handler. Using continue can often also help make large programs much more efficient. It's possible to end up with long sections of code that you only require in certain situations, so you could use continue to skip these when suitable.

continue is an excellent way of exercising more control over your scripts, hence why it's called a control statement. Whenever continue is triggered, it will skip to the end of whatever loop it's inside. In cases where you're working with nested loops, continue will only cut to the end of the inner-most loop. In terms of its applications, continue can be great for handling error cases. It's also a great way to skip past unrequired code segments, making your Python programs much more efficient.

gonzalezonswat.blogspot.com

Source: https://www.learndatasci.com/solutions/python-continue/

0 Response to "Continue With Loop With Error Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel