【问题标题】:Python Credit Card Check Function not running correctlyPython信用卡检查功能未正确运行
【发布时间】:2022-10-04 16:22:52
【问题描述】:

尝试在 Python 中实现一个程序,该程序可以检查输入是否是有效的信用卡并显示信用卡类型(VISA/MASTERCARD/AMEX)。对于 2 个值,它显示不正确,并且 2 天我不知道如何修复它?

收到的输出:(最后 2 个不正确 4111111111111113 和 4222222222223)我猜这与 check_last_digit 函数有关....

identifies 378282246310005 as AMEX
:) identifies 371449635398431 as AMEX
:) identifies 5555555555554444 as MASTERCARD
:) identifies 5105105105105100 as MASTERCARD
:) identifies 4111111111111111 as VISA
:) identifies 4012888888881881 as VISA
:) identifies 4222222222222 as VISA
:) identifies 1234567890 as INVALID
:) identifies 369421438430814 as INVALID
:) identifies 4062901840 as INVALID
:) identifies 5673598276138003 as INVALID
:( **identifies 4111111111111113 as INVALID**
    **expected "INVALID\n", not "VISA\n\n"**
:( **identifies 4222222222223 as INVALID**
    **expected "INVALID\n", not "VISA\n\n**

我的代码:

#function to find out the sum of every other numbers from the input card
def odd_sum(num):
    num = list(str(num))
    my_list_digits = [int(item) * 2 for item in num][::2]
    total_odd = 0
    for number in my_list_digits:
        if number > 9:
            first_digit = number // 10
            second_digit = number % 10
            total_odd += first_digit + second_digit

        else:
            total_odd += number
    return total_odd


# function to find out the sum of the other remaining numbers
def even_sum(num):
    num = list(str(num))
    del num[::2]
    my_list_digits1 = [int(item) for item in num]
    total_even = 0
    for item in my_list_digits1:
        total_even += item
    return total_even


# function to check the lenght of the input card
def check_length(num):
    num = str(num)
    num_length = len(num)
    if 13 <= num_length <= 16:
        return True
    else:
        print("INVALID")

# function to check the last digit of the sum ( even and odd)
def check_last_digit(num):
    odd = odd_sum(num)
    even = even_sum(num)
    if (odd + even) % 10 == 0:
        return True
    else:
        return False


# function to determine the type of card that was provided
def card_type(card_num):
    card_num = str(card_num)
    AMEX = ["34", "37"]
    MASTERCARD = ["51", "52", "53", "54", "55"]
    VISA = ["4"]
    if (card_num[0:2]) in AMEX:
        print("AMEX")
    elif (card_num[0:2]) in MASTERCARD:
        print("MASTERCARD")
    elif (card_num[0]) == VISA[0]:
        print("VISA\n")
    else:
        print("INVALID")



# main program to run all the above created functions
def main():
    #get input from user
    card_num = int(input("CARD: "))
    #check if the bellow 2 functions are True and if so, run the card type function so we can see what type of card was usde(visa, mastercard,amex)
    if check_length(card_num):
        card_type(card_num)
    else:
        if check_last_digit(card_num):
            card_type(card_num)



main()

【问题讨论】:

  • 注意:在您的 check_length 中,您没有在 else 语句中返回值。您应该在print("INVALID") 后面添加return False
  • @Jakob,即使进行了此更改,它也不会更改 2 个输入的状态..
  • 我知道,但是您仍然应该这样做以避免将来出现问题并保持代码干净。

标签: python


【解决方案1】:

它看起来像在你的主要功能

#check if the bellow 2 functions are True and if so, run the card type function so we can see what type of card was usde(visa, mastercard,amex)
if check_length(card_num):
    card_type(card_num)
else:
    if check_last_digit(card_num):
        card_type(card_num)

应该检查check_lengthcheck_last_digit 是否都通过了。但是,在您的代码中,如果 check_length 返回 True,则不会运行第二次检查。您需要更改代码以确保两个测试都运行:

def main():
    #get input from user
    card_num = int(input("CARD: "))
    #check if the bellow 2 functions are True and if so, run the card type function so we can see what type of card was usde(visa, mastercard,amex)
    if check_length(card_num) and check_last_digit(card_num):
        card_type(card_num)
    else:
        print("INVALID")

编辑

在您的Luhn algorithm 实现中还有另外两个错误。 odd_sum 函数应该对每隔一个数字求和,从最右边一,跳过一,但如果输入数字的位数是奇数,则您的实现对错误的数字求和。为了纠正这一点,您应该首先反转列表:

l = [int(item) * 2 for item in num[::-1]]

然后,删除第一个数字(最右边的一个)

l = l[1:]

最后,取每隔一个数字:

my_list_digits = l[::2]

然后,even_sum 中出现了类似的错误,您还应该考虑反转列表,以确保考虑正确的数字:

my_list_digits1 = [int(item) for item in num[::1]]
return sum(my_list_digits1)

最后,正如 Jakob 提到的,您应该在 check_length 函数中返回 False 而不是 "INVALID"

作为一个重构技巧,如果你看到自己在写

if condition:
    return True
else:
    return False

然后你可以在清洁器中重构它:

return condition

例如:

def check_last_digit(num):
    odd = odd_sum(num)
    even = even_sum(num)
    return (odd + even) % 10 == 0:

【讨论】:

  • 编辑后:(将4222222222222识别为VISA预期“VISA ”,而不是“无效 ":( 将 378282246310005 识别为 AMEX 预期 "AMEX ”,而不是“无效 " :( 将 371449635398431 识别为 AMEX 预期 "AMEX ”,而不是“无效 "
猜你喜欢
  • 1970-01-01
  • 2011-12-07
  • 2017-06-21
  • 2019-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多