Home >Backend Development >Python Tutorial >Python3通过Luhn算法快速验证信用卡卡号的方法

Python3通过Luhn算法快速验证信用卡卡号的方法

WBOY
WBOYOriginal
2016-06-06 11:16:152539browse

本文实例讲述了Python3通过Luhn算法快速验证信用卡卡号的方法。分享给大家供大家参考。具体分析如下:

Python3通过Luhn算法快速验证信用卡卡号,python用起来就是爽,很简单的三行代码就可以验证信用卡卡号是否有效

def luhn_check(num):
  ''' Number - List of reversed digits '''
  digits = [int(x) for x in reversed(str(num))]
  check_sum = sum(digits[::2]) + sum((dig//10 + dig%10) for dig in [2*el for el in digits[1::2]])
  return check_sum%10 == 0
if __name__ == "__main__":
  print(luhn_check(543298376))

希望本文所述对大家的Python3程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn