# Create Check Digit [closed]

I'm trying to create check digits and append them after the UPCs.

The data looks like this:

Because there're leading 0's in the front, I have to read it as a character:

import pandas as pd

upc = pd.read_csv("/Users/lee/Desktop/upc.csv", dtype = str)



Here's the check digit calculation algorithm:
(1) sum up odd digits; sum up even digits then multiply by 3; then sum of both sums.
(2) take this result module 10.
(3) if the result in not 0, substract from 10, this is the check digit; if the results is 0, then 0 is the check digit.

for example: upc 003459409000
step (1) 0 + 30 + 3 + 34 + 5 + 39 + 4 + 30 + 9 + 30 + 0 + 30 = 60
step (2) 60 mod 10 = 0
step (3) check digit = 0

Based on the algorithm, here's the code:

def add_check_digit(upc_str):
"""
Returns a 13 digit upc-a string from an 12-digit upc-a string by adding
a check digit
'0026065000069'
'0033558000109'
'0026400275305'
"""

upc_str = str(upc_str)
if len(upc_str) != 12:
raise Exception("Invalid length")

odd_sum = 0
even_sum = 0
for i, char in enumerate(upc_str):
j = i+1
if j % 2 == 0:
even_sum += int(char)
else:
odd_sum += int(char)

total_sum = (even_sum * 3) + odd_sum
mod = total_sum % 10
check_digit = 10 - mod
if check_digit == 10:
check_digit = 0
return upc_str + str(check_digit)


If I use this function, I get this:

Now my questions are:

1. This function works only for a single upc, i.e., I have to copy/paste each single upc and get the check digit.
How do I create a function that works for a list of UPCs in a dataframe? Each result should return a 13-digit UPC with the check digits appended after original UPC.
2. The UPCs are read as strings. How do I apply the function to the UPCs? I suppose I should convert the strings to numbers somehow. I'm really new to python.
3. After I get the new UPCs, how do I save the result in a csv file?

Thank you very much for your help.