Have you ever wondered how websites validate your credit card number when you shop online? They don’t check a massive database of numbers, and they don’t use magic. In fact, most credit providers rely on a checksum formula for distinguishing valid numbers from random collection of digits (or typing mistakes).
In this lab, you will implement a validation algorithm for credit cards. The algorithm follows these steps:
Double the value of every second digit beginning with the rightmost.
Add the digits of the doubled values and the undoubled digits from the original number.
Calculate the modulus of the sum divided by 10.
If the result equals 0, then the number is valid.
def is_valid(a): sum = 0 if len(a)%2==0:#判断信用卡位数，偶数位 for i in range(0,len(a)):#遍历所有 if i % 2 == 0:#需要*2的位数