Python GTIN number code

Python GTIN number code - Enter a 7 digit number to find the check digit or enter an 8 digit number to check the validity.


while 2>1:
    gtinnumber = input("Enter a 7 digit number to find the check digit or enter an 8 digit number to check the validity. ")
    if gtinnumber.isdigit()==False: 
continue
    
    flip=3
    total = [int(x) for x in gtinnumber]
    for loop in range(len(total)):
        total[loop]*=flip
        flip=-1*flip+4
        
    if len(str(gtinnumber))==8 and sum(total)%10==0: 
print("Valid GTIN-8 number")
    elif len(str(gtinnumber))==7:
        if sum(total)%10!=0: 
flip=((sum(total) + 9) // 10 * 10) - sum(total) + 1
        print(flip-1)
    else: print("Invalid number")


Learn More :