首页 > 解决方案 > 我需要在 Python2 中制作一个 EAN-13 号码有效性检查器。我不明白为什么这不起作用

问题描述

inp = raw_input("Input a EAN-13 number.")
aaa = False
bbb = False

if len(inp) == 13:
    bbb = True
else:
print "Error input"
exit

ean_number = int(inp)
def ean13(value_1):
mult_of_ten = 0
sum_of_digits = 0
done = False
for z in len(value_1):
    if not z == 0:
        if z % 2 == 0:
            value_1[z] *= 3
        elif not z % 2 == 0:
            value_1[z] *= 1
for a in len(value_1):
    sum_of_digits += value_1[a]
if sum_of_digits % 10 == 0:
    result = 0
elif not sum_of_digits % 10 == 0:
    while done == False:
        mult_of_ten = sum_of_digits
        for d in True:
            mult_of_ten += d
            if sum_of_digits % 10 == 0:
                done == True
    result = mult_of_ten - sum_of_digits
    if result == value_1[12]:
        print "True"

if bbb == True:
    ean13(ean_number)

我真的不明白为什么老师也不能帮助。

我需要在 Python2 中制作一个 EAN-13 号码有效性检查器。我不明白为什么这不起作用。任何人都可以帮忙吗?

标签: python

解决方案


我在 Python2 中制作了一个 EAN 13 号码有效性检查器。希望它可以帮助你。

# ean = '9780201379624'
ean = raw_input("Input a EAN-13 number:\n")
err = 0
even = 0
odd = 0
check_bit = ean[len(ean)-1]#get check bit(last bit)
check_val = ean[:-1]#Get all vals except check bit

if len(ean) != 13:#Check the input length
    print "Invalid EAN 13"
else:
    for index,num  in enumerate(check_val):#Gather Odd and Even Bits
        if index%2 == 0:
            even += int(num)
        else:
            odd += int(num)
    if ((3*odd)+even+int(check_bit)) % 10 == 0:# Check if the algorithm 3 * odd parity + even parity + check bit matches
        print "Valid EAN 13"
    else:
        print "Invalid EAN 13"

推荐阅读