python信用卡管理_编写python代码检查信用卡并禁止

本文介绍了一个Python脚本,该脚本通过Luhn算法验证信用卡号的有效性,并能识别不同类型的信用卡,如Visa、Master Card等。然而,脚本在运行时遇到了一个未明确的错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我正在编写一个小脚本来检查信用卡和银行的有效性,但由于一个疯狂的原因,我得到了一个我不理解的错误。在#!/usr/bin/python

# -*- coding: utf-8 -*-

def digitSum(myString):

length = len(myString)

oddSum = 0

evenSum = 0

#base case

if (length ==0):

return 0

#length > 0

else:

#if even

if (length % 2 ==0):

last = int(myString[-1])

evenSum += last

return evenSum + digitSum(myString[:-1])

else:

last = int(myString[-1])

last = 2 * last

part_sum = last // 10 + last % 10

oddSum += part_sum

return oddSum + digitSum(myString[:-1])

def luhns():

myString = input("Entrez les 16 numéros de la Carte de Crédit ")

total = digitSum(myString)

if (total % 10 == 0):

if total[:1] == "4":

cardtype = "Visa"

if int(total[:2]) >= 51 and int(total[:2]) <= 55:

cardtype = "Master Card"

if total[:2] == "34" or total[:2] == "37":

cardtype = "American Express"

if total[:4] == "6011":

cardtype = "Discover"

if total[:2] == "36":

cardtype = "DINERS"

if int(total[:3]) >= 300 and int(total[:3]) <= 305:

cardtype = "DINERS"

return cardtype

print ('Carte valide') + cardtype

else:

print ('Carte invalide')

def main():

luhns()

#Python call to main()

main()

这是我得到的错误:

^{pr2}$

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值