Pete Apollo
Pete Apollo

Reputation: 11

Append digit to split list

This is my program to calculate the eighth digit of a GTIN-8 number.

The goal was to create a list which the user can input 7 digits to, split the list into seperate values, multiply digits 1,3,5,7 by 3 and add them to the rest.

    origSevList = []

    def enterDig():
        global origSev
        origSev = input("Please enter your seven digit number for your GTIN-8 code ")
        origSevList.append(origSev)
        return origSevList

    def splitList(origSevList):
        global item
        for item in origSevList:
            item.split(',')
            origSevList = [item[0], item[1], item[2], item[3], item[4], item[5], item[6]]
        print (("Inputted seven digits  number split in a list"), origSevList)

    def xThree(origSevList):
        global xByThree
        xByThree = int(item[0])*3 + int(item[2])*3 + int(item[4])*3 + int(item[6])*3

    def xOne(origSevList):
        global xByOne
        xByOne = int(item[1]) + int(item[3]) + int(item[5])

    def addOneThree(origSevList):
        global addSev
        addSev = xByThree + xByOne
        print (("The sum of your seven digits mulitplied alternately by 1 and 3 ="), addSev)

Next was to find the eighth digit

    def eighthDigit(origSevList):
        global eighth
        roundNum = ((addSev + 9) // 10*10)
        eighth = roundNum - addSev
        print (("Your eighth digit is"), roundNum - addSev)
        print ((addSev + 9) // 10*10)

    enterDig()
    splitList(origSevList)
    xThree(origSevList)
    xOne(origSevList)
    addOneThree(origSevList)
    eighthDigit(origSevList)

Now what I need to do is append the eighth digit to the list and print it to get the full GTIN8 number. Any ideas on how to do that? I'm a beginner please excuse my messy code

Upvotes: 1

Views: 736

Answers (2)

EbraHim
EbraHim

Reputation: 2349

I guess this is such a thing that you want:

def func():
    sum = 0
    number = raw_input("7digit? ")
    for i in range(len(number)):
        if i%2 ==0:
            sum += int(number[i]) * 3
        else:
            sum += int(number[i])
    GTIN8 = int( round(sum, -1)- sum) % 10
    return number+ str(GTIN8)

out = func()
print out

Works as below:

>>> ================================ RESTART ================================
>>> 
7digit? 1234567
12345670

In general:

If you want to add a letter to a string: Simply use + character:

>>> a = "1"
>>> b = "12345"
>>> a + b
'112345'
>>> 

If you want to add a digit to a number in the left side:

>>> b = 12345
>>> c = b*10 + a
>>> c
123451
>>> 

And if you want to add an element to a list:

>>> a = 1
>>> b = [1,2,3]
>>> b.append(a)
>>> b
[1, 2, 3, 1]
>>> 
>>> 
>>> a = "1"
>>> b = ["1", "2", "3"]
>>> b.append(a)
>>> b
['1', '2', '3', '1']
>>> 

Upvotes: 0

Preyas Shah
Preyas Shah

Reputation: 14

Answer by EbraHim might serve your purpose. I have some additional feedback to make code more robust.

Put all int type conversions in try and catch block, so if user has not input the digit 0-9, the code will be able to handle properly and give error message (exit gracefully instead of throwing exception). Also, you can check if user has entered 7 digits, using len() function, so you can give error message right away if user has input more or less than 7 characters.

Also, why you are appending the origSev to origSevList? you will get 7 digit number in origSev. you can access the individual digit by origSev[i], convert to int and process as you want.

Thanks!!

Upvotes: 0

Related Questions