我是Python编程的初学者,我接受了我在互联网上发现的挑战(信用卡号验证器{Luhn算法}),我写了这个函数:
def CheckNumber(CreditCardNumber) :
CheckNumberBool = False
CreditCardNumber = list(map(int, CreditCardNumber))
x = 1
y = 0
WorkingList = list()
#--------------------------------------------------#
while x <= 15 :
WorkingList[x].append(CreditCardNumber[x] * 2)
x += 2
#--------------------------------------------------#
while y <= 15 :
WorkingList[y].append(CreditCardNumber[y])
y += 2
#--------------------------------------------------#
WorkingListStr = str(sum(WorkingList))
#--------------------------------------------------#
while y <= 15 :
if WorkingList[y] >= 10 :
for x in WorkingListStr :
WorkingList.append(int(x))
y += 1
#--------------------------------------------------#
if sum(WorkingList) % 10 == 0 :
CheckNumberBool = True
print("This is a Valid Credit Card Number")
else :
CheckNumberBool = False
print("This isn't a Valid Credit Card Number")
但问题是,当我在终端命令行(python3 myfile.py)中运行它时,我收到此错误:
File "Credit_Card_Algorithm.py", line 13, in CheckNumber
WorkingList[x].append(CreditCardNumber[x] * 2)
IndexError: list index out of range
感谢您提出的所有非常有用的建议,如果我犯了错误,抱歉,这是我第一次写信给巨大的StackOverflow社区;-D