所以基本上,我需要为 luhn 算法编写代码,但我需要至少有 5 个函数
我已经编写了代码来执行此操作,但我却在本应为真的地方收到错误的结果。例如,我知道数据文件中的第二个和第四个数据片段对于这个算法是正确的,但我的所有输出都是错误的。你能帮我找出我哪里做错了吗?
这是代码:
def CheckLength(numb):
if len(numb)>12 and len(numb)<17:
return True
else:
return False
def CheckType(numb):
if numb[0]=='4':
return 'Visa'
elif numb[0]=='5':
return 'MasterCard'
elif numb[0]=='6':
return 'Discover'
elif numb[0:2]=='37':
return 'American Express'
else:
return 'Invalid Entry'
def Step1(numb):
total1=0
total2=0
length=len(numb)
for i in range(length-2,-1,-2):
double=eval(numb[i])*2
if double>9:
doublex=str(double)
doubleY=int(doublex[0])+int(doublex[1])
total1+=doubleY
else:
total2+=double
total=total1+total2
return total
def Step2(numb):
total=0
length=len(numb)
for i in range(length-1,-2,-2):
total+=i
return total
def Step3(num1,num2):
total=num1+num2
if total%10==0:
return True
else:
return False
def main():
inFile=open('pa7.cards','r')
cardNum=inFile.readline().strip()
while cardNum!='99999':
step1=Step1(cardNum)
step2=Step2(cardNum)
step3=Step3(step1,step2)
print(step1)
print(step2)
print(step3)
cardNum=inFile.readline().strip()
inFile.close()
main()
这是数据文件:
4388576018402626
4388576018410707
37271983
5190828258102121
99999
这是我在打印所有 3 个步骤时得到的输出
4
63
False
0
63
False
7
15
False
4
63
False
最佳答案
(我的评论作为答案,包括来自@DavidZemens评论的建议和更正)
在 bug 方面,我认为你有:
第 1 步在循环内有 return
语句,因此循环只发生一次,然后停止。
def Step1(numb):
for i in range(length-2,-1,-2):
return total
^ -- ^ -- move this left, to where 'for' is
第 2 步是计算计数器的总和,而不是信用卡数字,它是逐步遍历每隔个数字而不是每个数字:
def Step2(numb):
total=0
length=len(numb)
for i in range(length-1,-1,-1):
total += int(numb[i])
return total
在一般的代码注释中,这种测试:
if len(numb)>12 and len(numb)<17:
return True
else:
return False
有点多余。 If (truth test) return True else return False可以变成return (truth test):
return 12 < len(numb) < 17
及以后:
total = num1 + num2
if total%10==0:
return True
else:
return False
可以是:
return (num1 + num2) % 10 == 0
这个计算:
double=eval(numb[i])*2
if double>9:
doublex=str(double)
doubleY=int(doublex[0])+int(doublex[1])
total1 += doubleY
将数字转换为文本有点尴尬,获取文本字符,将它们转换回数字,然后将它们相加。
它的作用是取十位数(整数除以 10)和余数(模 10),因此您可以将其全部保留为数字:
double = int(numb[i]) * 2
if double > 9:
total1 += (double // 10) + (double % 10)
eval()
是一种不好的做法,它使任何输入都可以完全访问 Python 解释器,而没有任何安全措施。在您的代码中,您可以使用 int()
代替
你的文件循环可以更清晰:
def main():
with open('pa7.cards') as inFile:
for cardNum in inFile:
cardNum = cardNum.strip()
step1=Step1(cardNum)
step2=Step2(cardNum)
step3=Step3(step1,step2)
print(step1)
print(step2)
print(step3)
您可以通过将 cardNum 一次转换为数字列表然后使用它来获得一些好处,而不是到处调用 int()
。
您还可以优化这两个未使用的函数(假设您在代码的其他地方使用它们):
def CheckLength(numb):
return 12 < len(numb) < 17
def CheckType(numb):
"""
returns the type of credit card, based on the first digit or 2 digits
numb; passed as string
"""
n = numb[0] if not numb[0] == '3' else numb[:1]
d = {4:'Visa',
5:'Mastercard',
6:'Discover',
37:'American Express'}
return d.get(int(n), 'Invalid Entry')
关于python - 需要代码方面的帮助,所有结果都返回错误,而 2 应该为真,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33704519/