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

2024-07-08 09:56:11 发布

您现在位置:Python中文网/ 问答频道 /正文

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中的错误是错误的,但答案是正确的。请推荐给我


Tags: and答案numberonlyreturnifisdef
1条回答
网友
1楼 · 发布于 2024-07-08 09:56:11

您必须查看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

相关问题 更多 >

    热门问题