python-3.x 信用卡查询练习

fhg3lkii  于 2023-02-06  发布在  Python
关注(0)|答案(4)|浏览(125)

对于一个免费的在线Python教程,我需要:
编写一个函数来检查给定的信用卡号是否有效。函数check(S)应该将字符串S作为输入。首先,如果字符串不符合"#### #### #### ####"(其中每个#都是一个数字),则它应该返回False。然后,如果数字之和可以被10整除(“校验和”方法),则过程应返回True,否则应返回False。例如,如果S是字符串"9384 3495 3297 0123",则尽管格式正确,但数字和为72,因此应返回False
下面显示了我所得到的结果。我认为我的逻辑是正确的,但不太明白为什么它给了我错误的值。是代码中有结构问题,还是我使用了错误的方法?

def check(S): 
    if len(S) != 19 and S[4] != '' and S[9] != '' and S[14] != '':
        return False                # checking if the format is correct

    S = S.replace(" ",'')         # Taking away spaces in the string
    if not S.isdigit():
        return False             # checking that the string has only numbers

    L = []            
    for i in S:
        i = int(i)                # Making a list out of the string and converting each character to an integer so that it the list can be summed 
        L.append(i)
    if sum(L)//10 != 0:        # checking to see if the sum of the list is divisible by 10
        return False
kkih6yb8

kkih6yb81#

我们不是在测试空格,而是在测试 * empty * 字符串,当在python字符串上使用直接索引时,您永远不会发现这一点。
此外,如果这4个条件中的任何一个为真,就应该返回False,而不是如果它们同时为 * 所有 * 真:

if len(S) != 19 or S[4] != ' ' or S[9] != ' ' or S[14] != ' ':
    return False

接下来,替换空格,但不要再检查长度。如果我给你19个空格:

S = S.replace(" ", '')
if len(S) != 16 or not S.isdigit():
    return False

最后,您需要首先收集所有数字,并检查是否存在 * 余数 *:

L = map(int, S)  # makes S into a sequence of integers in one step
if sum(L) % 10 != 0:  # % calculates the remainder, which should be 0
    return False

如果所有测试都通过了,不要忘记返回True:

return True

在最后。
把所有这些放在一起,你会得到:

>>> def check(S):
...     if len(S) != 19 or S[4] != ' ' or S[9] != ' ' or S[14] != ' ':
...         return False
...     S = S.replace(" ", '')
...     if len(S) != 16 or not S.isdigit():
...         return False
...     L = map(int, S)  # makes S into a sequence of integers in one step
...     if sum(L) % 10 != 0:  # % calculates the remainder, which should be 0
...         return False
...     return True
... 
>>> check('9384 3495 3297 0123')
False
>>> check('9384 3495 3297 0121')
True
whlutmcx

whlutmcx2#

下面是一个基于正则表达式的方法:

import re
def cc(pat):
    # check for the pattern #### #### #### #### with each '#' being a digit
    m=re.match(r'(\d{4})\s(\d{4})\s(\d{4})\s(\d{4})$', pat.strip())
    if not m:
        return False
    # join all the digits in the 4 groups matched, 
    # turn into a list of ints, 
    # sum and 
    # return True/False if divisible by 10: 
    return sum(int(c) for c in ''.join(m.groups()))%10==0

>>> cc('9384 3495 3297 0123')
False
>>> cc('9384 3495 3297 0121')
True
y0u0uwnf

y0u0uwnf3#

这是我的方法,这是一个有点长的代码,但我喜欢使用定义函数。由于某种原因,代码不工作的计算机科学界的网站,但它的工作在PyCharm程序。

def CardNumber():
        global card     #  Making variable global for function SumCardNumDigits(); see below
        card = input()  # Credit card number is entered
        return card

    def check(S):
        CardNumber = S
        SplitCardNum = CardNumber.split()      # Split credit card number into a list, so we get [####, ####, ####, ####]
        for i in range(0, len(SplitCardNum)):  # Checking if each item in list has length of four strings and each item is
                                               # an actual a number

            if len(SplitCardNum[i]) == 4 and SplitCardNum[i].isdigit():
            SplitCardNum.insert(i, True)   # We add to a list a True value at position i
                del SplitCardNum[i + 1]    # We delete items at position i + 1
            return SplitCardNum

    def checkFormat(SplitCardNum):
        if SplitCardNum == [True] * 4:  # Checking if all above conditions are met in function check(S)
                                        # So the returned value from previous function is a list [True, True, True, True]
            return True
        else:
            return False

    def SumCardNumDigits():
        Ncard = card                    # Using global variable 'card' from function CardNumber()
        SumN = 0
        for i in Ncard:            # Summing up all digits in string 'Ncard', if i position in a string is empty space " "
                                   # we skip a step.
            if i == " ":
                continue
            else:
                SumN += int(i)
            return SumN

    def DivideByTen(SplitCardNum):
        if SplitCardNum == True:        # If conditions from function check(S) are met, we divide the sum of digits
                                        # of credit card number by 10
            SumNumber = SumCardNumDigits() % 10  # We do this by using the returned value of function SumCardNumDigits()
            global SumNumber       # <--- Fixed code
            return SumNumber
        else:
            return False

    def IsDivideByTen(SumNumber):
        check = checkFormat(SplitCardNum)      # Finally we check if the sum of digits of credit card number is divisible by 10
        if SumNumber == 0 and check == True:   # <--- Fixed code
            return True
        else:
            return False

    print(IsDivideByTen(DivideByTen(checkFormat(check(CardNumber())))))  # Output final result

    # You can test this code at: http://cscircles.cemc.uwaterloo.ca/visualize/#mode=edit  and see how it works.
    # Try for '9384 3495 3297 4523' and '9384 3495 3297 4526'
pobjuy32

pobjuy324#

这是我的方法,目前正在学习Python,还没有看到这样的答案:

def check(S):
   if len(S) != 19 or len(S[4:19:5].strip()) != 0:
      return False
   digits = S.replace(" ", '')
   if not digits.isdigit():
      return False
   sum = 0
   for digit in digits: 
      sum += int(digit)
   if sum % 10 == 0:
      return True
   else:
      return False

相关问题