当且仅当一个数字具有至少两个连续的数字且每个数字等于3或该数字的最后一个数字可被4整除时,该数字才是苍白的

问题描述

def pale(n):
    '''(int)->bool
    return A number is not pale if and only if it has at least two consecutive digit divisible by 4.
  
    >>> pale(1128)
    Fasle
    >>> pale(3443)
    True
    '''
    return n%4!=0 and n!=33
   
print(pale(5433))

但是答案5433中的某些错误错误的,但答案是正确的。请推荐给我。

解决方法

您必须查看n的每个数字,而不是整数:

def pale(n):
    sym = str(n)
    for idx in range(len(sym)-1): # For each digit in the number n
        if int(sym[idx])%4 == 0: # if the digit is divisible by 4
            if int(sym[idx+1])%4 == 0: # and if the next one is divisible by 4
                return True
    return False