I'm trying to create a program that checks the validity of a UPC code of any length (although most are 12 digits) based on a string of numbers entered by the user. My current approach is first storing the string into a list and then iterating through the list using "for in range."
The guidelines I am using for how UPC code validation algorithms work is as follows: (The UPC codes are designed this way in part to prevent cashiers making a typo at checkout, as one wrong digit will likely make the code invalid)
The validation process runs from RIGHT to LEFT.
For digits in even positions, there are no changes.
For digits in odd positions, multiply the digit by 3.
Sum the results.
For a valid UPC number, this sum will be a multiple of 10.
I have been able to get everything running as I have hoped with the exception of guideline 1, that the validation process occurs from right to left across the string of numbers in the UPC code. My current method goes from left to right. This will prove to be an issue if a user-entered UPC code was an odd number of digits long, as it would throw off the algorithm.
The input code
in def upc_check(code):
below is the UPC code the user enters to see if the UPC code is valid.
def upc_check(code):
ODD_DIGIT_MULTIPLIER = 3
UPC_VALIDITY_DIVISOR = 10
digits = []
for num in code:
digits.append(int(num))
print(digits)
check_sum = 0
for i in range(0, len(code)):
if (i + 1) % 2 != 0:
check_sum += (ODD_DIGIT_MULTIPLIER * digits[i])
else:
check_sum += (digits[i])
if check_sum % UPC_VALIDITY_DIVISOR == 0:
return "Valid UPC code."
else:
return "Invalid UPC code."
Currently, the output is as I expect it (i.e. a valid UPC code shows up as valid and an invalid UPC code shows up as invalid) but the algorithm is going from left to right through the string of numbers.
To go from right to left, I would expect that I could make the following replacement and achieve the same results:
for i in range(0, len(code)):
with
for i in range(-1, -len(code), -1):
This would make the code read as follows:
def upc_check(code):
ODD_DIGIT_MULTIPLIER = 3
UPC_VALIDITY_DIVISOR = 10
digits = []
for num in code:
digits.append(int(num))
print(digits)
check_sum = 0
for i in range(-1, -len(code), -1):
if (i + 1) % 2 != 0:
check_sum += (ODD_DIGIT_MULTIPLIER * digits[i])
else:
check_sum += (digits[i])
if check_sum % UPC_VALIDITY_DIVISOR == 0:
return "Valid UPC code."
else:
return "Invalid UPC code."
However, when I make this change all my valid UPC codes now return the value invalid, and I am unsure why. I would expect the output to be the same. Does anyone have any suggestions? Thank you for any help!
If it is helpful for checking, the following are valid UPC codes:
096619363148
017082886026
381370036005
Here are some invalid UPC codes (Intentional typos from codes above):
096619363149
017082786026
381370036015