简体   繁体   English

处理异常后继续在 for 循环中进行下一次迭代

[英]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.一旦达到 2.7,它就会一遍又一遍地打印“错误”。 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).确实会继续下一次迭代(甚至不需要continue语句来实现这一点)。

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 .问题是在下一次迭代中,同样的事情发生了——因为num = int(num)失败, try块中的代码实际上没有发生,因此numnum_reverse的值没有变化。

It is clear that your num_list actually contains strings - despite your description - since otherwise the num_reverse = num[::-1] would fail.很明显,您的num_list实际上包含字符串 - 尽管有您的描述 - 因为否则num_reverse = num[::-1]会失败。 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).当输入字符串不能被解释为整数时,您似乎想要做的是移动到for循环的下一次迭代(如果第一次可以,那么它应该继续进行所有的 while 循环迭代)。

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.) (请注意,只要an_integer实际上是int ,这应该始终有效,因为在被转换的字符串中允许前导零。)

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一个简单的解决方案是用break替换您的 continue 另外我认为您打算将您的num变量转换为 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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