信用卡卡号检查

********检查给定的信用卡卡号是否有效*********

信用卡卡号格式:"#### #### #### ####"每个#号代表一个数字,所有数字相加的和能被10整除

满足格式返回True否则返回False

具体代码如下:

def check(S):
   s=S.split(' ')#将输入的字符串去掉空格转换成列表
   r=0
   if len(s)==4 and \
      len(s[0])==4 and \
      len(s[1])==4 and \
      len(s[2])==4 and \
      len(s[3])==4:#保证每个列表元素的长度为4
      for i in s:#循环列表获得每个元素的字符串
         if i.isdigit()==True:#判断该字符串是否是数字组成,如果是则获取该列表的每个数字
            for j in i:
               r+=int(j)#将每个数字相加的和赋值给r
      if r%10==0:#如果所有数字相加能与10整除则返回True
         return True
      else:return False
   else:return False

猜你喜欢

转载自blog.csdn.net/qq_39299636/article/details/80511297