python - 我需要在 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 号码有效性检查器。我不明白为什么这不起作用。任何人都可以帮忙吗?
解决方案
我在 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"
推荐阅读
- java - Powershell作为unix系统上的java进程,cmdlet不显示输出
- flutter - 添加多个图像
- python - Python:在 requirements.txt 中引用分叉项目
- android-studio - Flutter:NoSuchMethodError:“getter 'month' was called on null”是什么意思?(日期时间包)
- c - 未定义对“sqrt”、“log”的引用,
- solace - 安慰保证消息发布问题
- linux - 免费命令:哪一列最相关?
- javascript - 不能从上到下交换 div。否则有效
- lisp - 将二进制字符串转换为数字
- java - 使用 Apache poi 将公式应用于 excel 单元格时,内容覆盖在另一个之上