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'這樣的東西,它按順序給出了行數,而不是列數和列表解析。 –
當您修復了代碼格式時,請對我進行平郵,然後我會給您寫回答。 –