当我用我的代码检查ISBN(DIGIT)时:check_digit_13(2000003294107)
def check_digit_13(isbn):
assert len(isbn) == 12
sum = 0
for i in range(len(isbn)):
c = int(isbn[i])
if i % 2: w = 3
else: w = 1
sum += w * c
r = 10 - (sum % 10)
if r == 10: return '0'
else: return str(r)
错误:
Traceback (most recent call last):
File "parser.py", line 16, in <module>
lk.run(document)
File "/data/www/crons/lk/parser.py", line 33, in run
field = lkmapper.all(row, document)
File "/data/www/crons/mappers/lk.py", line 5, in all
print isbn.check_digit_13(field[0])
File "/data/www/crons/libs/isbn.py", line 13, in check_digit_13
assert len(isbn) == 12
AssertionError
答案 0 :(得分:24)
检查您的功能输入。它是一个字符串?然后计算字符数。多少? 12?我算了13。
答案 1 :(得分:5)
assert len(isbn) == 12
应为assert len(isbn) == 13
。毕竟,当前的ISBN代码长度为13位,您的示例输入也是如此。也许你感到困惑,因为索引从0开始,但长度是长度,所以它是13。