Skip to main content
Code Review

Return to Question

Question Protected by Community Bot
edited tags
Link
200_success
  • 145.5k
  • 22
  • 190
  • 478
Source Link

Check Digit Calculator & GTIN-8 Validator

This program calculates the 7th digit of a GTIN-8 barcode and calculates the validity of an 8 digit GTIN-8 barcode. Could I simplify my code any further?

import math 
def roundup(x): 
 return int(math.ceil(x / 10.0)) * 10 
def doMaths(number): 
 count = 0
 total = 0 
 for n in number: 
 n = int(n) 
 if count %2 == 0: 
 total += n * 3 
 else: 
 total += n 
 count += 1
 if len(number) == 8:
 if total %10 == 0: 
 print("Valid")
 else:
 print("Invalid")
 else:
 print(number + str(roundup(total) - total)) 
while True:
 try:
 number = input("Enter a 7 or 8 digit number for testing: ")
 i = int(number)
 if len(number) == 7 or len(number) == 8:
 doMaths(number)
 break
 else:
 print("Length of number must be 7 or 8")
 continue
 except ValueError:
 print("Only enter numbers.")
 continue
lang-py

AltStyle によって変換されたページ (->オリジナル) /