n数値の文字列になる変数を含む関数を定義しようとしています。nたとえば"3884892993"、関数の定義はとして始まりますがis_true(n)、nが文字列になる場合はis_true(n)、文字列を定義すると、次のようになります。などの文字列の例を使用して関数をテストしますn = "3884892993"。is_true(n)ただし、使用すると構文エラーが発生します。そして、nの文字列の例を使用して、この関数をどのようにテストするのか疑問に思っています。
定義する私の関数全体をここに示します:http://oi44.tinypic.com/282i3qo.jpgしかし、私は絶対的な初心者なので、おそらく多くの間違いがあることを覚えておいてください。可能な限り:)
def is_valid("n"): #n is the number to be checked.
number =
[int(y) for y in A] #converts the string into a list of useable digits.
altern1 = integer[-2::-2] #sets altern1 as one set of alternating digits.
double = [x*2 for x in altern1] #doubles each element of the list altern1.
sum1 = sum(double) # adds together all the doubled items of the list.
altern2 = integer[-1::-2] #sets altern2 as the other set of alternating digits.
return sum2 = sum(altern2)#sums the other set of alternating digits.
sumtotal = sum1 + sum2 #works out the total sum to be worked with.
for mod = sumtotal % 10: #works out remainder when sumtotal is divided by 10
if mod == 0 : #if remainder is zero sumtotal is a multiple of 10
print 'True' #sumtotal is a multiple of 10 therefore n is a credit card number
else:
print 'False' #sumtotal is NOT a multiple of 10 therefore not a valid credit card number
実際の質問は次のとおりです。
数値を検証するためのアルゴリズムは次のとおりです。(a)最後から2番目の桁から始めて、最初の桁に向かって、各交互の桁を2倍にします。(b)2桁を合計し、13を1 + 3などとして扱い、その結果を2桁の合計に加算します。(c)合計が10で割り切れる場合、その数値は有効なクレジットカード番号です。
クレジットカード番号を文字列として引数として取り(たとえば、valid( "49927398716"))、その番号が有効なクレジットカード番号であるかどうかに応じてTrueまたはFalseを返す関数is_valid()を記述してテストします。