【发布时间】:2011-10-26 20:52:52
【问题描述】:
我正在尝试定义一个包含变量n 的函数,其中n 将是一串数字,例如"3884892993",函数的定义以 is_true(n) 开头,但是如果 n 将是一个字符串,它应该是 is_true(n),然后一旦定义了字符串,我可以使用示例字符串测试函数,例如 @987654328 @。但是,当我使用 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) 从倒数第二个数字开始,一直到第一个数字,将每个交替的数字加倍。 (b) 将加倍的数字相加,将 13 视为 1+3 等,并将结果与未加倍的数字相加 数字 (c) 如果总和能被 10 整除,则该数字是有效的信用卡号。
编写并测试一个函数 is_valid(),该函数将信用卡号作为字符串作为参数 (例如有效(“49927398716”))并根据数字是否为 有效的信用卡号码。
【问题讨论】:
-
好吧,我只是认为使用剪切工具比将其粘贴到此处并确保它提供正确的缩进更容易(无论如何这可能是错误的)。这就是我的新手。它也应该是 PNG,对此感到抱歉。
-
能否请您打印出您的代码的 JPEG 文件,将其放在 wooden table 上,然后拍照?
-
@Adam Rosenfield 我认为那是讽刺哈哈,我已经放了原始代码。
-
@Adam 谢谢你的笑声。乔治:是的,那是讽刺;)但它非常友好。 “我们”(意思是 SO-ers)需要原始代码,以便我们可以复制/粘贴/运行您的代码。如果您发布我们可以以这种方式运行的内容,您的问题将获得更多(有用的)答案。欢迎来到 S.O.
-
@George:是的,这是个玩笑;我想如果没有链接,它可能会超出人们的想象。
标签: python string integer digits