L1-016 查驗身份證 python

豆豆是只乖狗狗發表於2024-06-29

python 實現

num=int(input())
id_lst=[]
for i in range(num):
    s=str(input())
    id_lst.append(s)

lst_right=[7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
lst_module=[1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2]
lst_result=[]

count=0
for item in id_lst:
    sum=0
    flag=0
    for j in range(len(item)-1):
        if item[j].isdigit():
            sum += int(item[j])*lst_right[j]
        else:
            flag=1
            break
    if flag==0:
        if item[len(item)-1]=="X":
            if lst_module[sum%11] == 10:
                count+=1
            else:
                print(item)
        else:
            if lst_module[sum%11]==int(item[len(item)-1]):
                count += 1
            else:
                print(item)
    else:
        print(item)
if count==num:
    print("All passed")

相關文章