问题描述
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