import numpy as np
def validation(x):
x = np.asarray(x)
if len(x) != 16:
return("Card doesn't have exactly 16 digits. Try again")
values = []
rwhat = x[::-1] # reverse the order of the credit card numbers
rwhat
checkDig = rwhat[0] # the leftmost [originally rightmost] digit which is the checkDigit ... I'm just doing this because it's easier for me to work with
checkDig
withCheck = [] # to append later when we add all single digits
everySec = rwhat[1:16:2] # we don't want to double the checkDigit, but we're extracting every second digit starting from the first, leftmost digit [tho we omit this checkDigit
everySec
def double(num): # to double the extracted second digit values
return [j * 2 for j in everySec]
xx = double(everySec)
xx
def getSingle(y): # to add the sum of the digits of any of the new doubled numbers which happen to be greater than 9
u = 0
while y:
u += y % 10
y //= 10
return u
yy=list(map(getSingle,xx))
yy
withCheck.append(checkDig)
withCheck
new_vv = withCheck + yy
new_vv # now we include the omitted checkDigit into this new list which should all be single digits
sumDig = sum(new_vv)
sumDig # now have the sum of the the new_vv list.
def final(f):
if sumDig % 10 == 0: # if the calculated sum is divisible by 10, then the card is valid.
return("Valid")
else:
return("Invalid")
go = final(sumDig)
values.append(go) # basically just appending into values[] for the sake of the validation(x) function, and so we can return something for this function. in this case we'd return values as seen below.
return values
所以我创建了这个程序,我需要弄清楚如何定义第一个(最外层)函数的参数将卡号作为一个由16列组成的多维数组,并最终返回一个列表的值表示“有效”或“无效”。将参数(x)指定为具有特定列数的多维数组?
def validation(x)
工作内的东西,我已经在实际制作上述函数之前测试过它,但我只是不知道如何指定这个函数[也就是这个程序基本上是]在一个多维数组中16列。
我敢肯定的代码行关于if len(x) != 16
是问题的一部分,但它的工作原理,如果我们只是想运行一个卡[16个位数又名一组]
例如,如果我想尝试validation(([[0,1,2,3,4,5,6,7,8,9,0,1,2,3,4,5],[1,2,3,4,5,6,7,8,9,0,1,2,3,4,5,6]])
我与输出困扰:"Card doesn't have exactly 16 digits. Try again"
,而不是正常运行,并返回我,指出在各自有效还是无效的列表,以每张卡
修复您的缩进。这是Python无效的表现。 –
如果您想使用numpy,请使用numpy。不要使用像'len'这样的东西,它按顺序给出了行数,而不是列数和列表解析。 –
当您修复了代码格式时,请对我进行平邮,然后我会给您写回答。 –