简体   繁体   中英

Continue to next iteration in a for loop after handling an exception

num_list = [195,265,2.7,750]

Once this reaches to 2.7, it just prints 'error' over and over. Is there a way to continue over the next iteration after handling this exception?

def palindrome(num_list):
    for num in num_list:
        num_reverse = num[::-1]
        count = 0
        while num != num_reverse:
            try:
                num = int(num)
                num_reverse = int(num_reverse)

                num += num_reverse
                count += 1

                num = str(num)
                num_reverse = num[::-1]
                print(count, num)
            except ValueError:
                print('Error')
            continue

It does continue to the next iteration (and the continue statement is not even needed to make this happen).

The problem is that in the next iteration, the same thing happens - because num = int(num) failed, none of the code in the try block actually happened, therefore there is no change to the values of num or num_reverse .

It is clear that your num_list actually contains strings - despite your description - since otherwise the num_reverse = num[::-1] would fail. It seems like what you want to do is move to the next iteration of the for loop when the input string can't be interpreted as an integer (if it can the first time, then it should continue working through all the while-loop iterations).

The easiest way to write correct code that is easily understood, is to have the values always be integers inside this loop. Make sure you start out with integers, and then make a separate function to reverse the value and give you an integer back :

def with_reversed_digits(an_integer):
    digits = str(an_integer)
    return int(digits[::-1])

(Note that this should always work as long as an_integer is actually an int , because leading zeroes are allowed in the string being converted.)

Now you can just write your logic appropriately:

for num in num_list:
    # If any of these values isn't an integer, that should be treated
    # as a programming error *elsewhere*, i.e. in the calling code.
    num_reverse = with_reversed_digits(num)
    count = 0
    while num != num_reverse:
        # We have no try/except or int/str conversions here,
        # because we always just have integers.
        num += num_reverse
        count += 1
        num_reverse = with_reversed_digits(num)
        print(count, num)

A simple solution would be to replace your continue with break Also i think you meant to cast your num variable as a str

def palindrome(num_list):
    for num in num_list:
        num = str(num)
        num_reverse = num[::-1]
        count = 0
        while num != num_reverse:
            try:
                num = int(num)
                num_reverse = int(num_reverse)

                num += num_reverse
                count += 1

                num = str(num)
                num_reverse = num[::-1]
                print(count, num)
            except ValueError:
                print('Error')
            break

Output

1 786
1 827
Error
1 807

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM