简体   繁体   English

将数字附加到拆分列表

[英]Append digit to split list

This is my program to calculate the eighth digit of a GTIN-8 number. 这是我计算GTIN-8号码第八位的程序。

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. 目标是创建一个用户可以输入7位数的列表,将列表拆分为单独的值,将数字1,3,5,7乘以3并将其添加到其余值。

    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. 现在我需要做的是将第八位数字附加到列表并打印它以获得完整的GTIN8编号。 Any ideas on how to do that? 有关如何做到这一点的任何想法? I'm a beginner please excuse my messy code 我是初学者,请原谅我凌乱的代码

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']
>>> 

Answer by EbraHim might serve your purpose. EbraHim的回答可能符合您的目的。 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). 将所有int类型转换放在try和catch块中,因此如果用户没有输入数字0-9,代码将能够正确处理并给出错误消息(优雅地退出而不是抛出异常)。 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. 此外,您可以使用len()函数检查用户是否输入了7位数,因此如果用户输入的字符多于或少于7个字符,您可以立即给出错误消息。

Also, why you are appending the origSev to origSevList? 另外,为什么要将origSev附加到origSevList? you will get 7 digit number in origSev. 您将获得origSev中的7位数字。 you can access the individual digit by origSev[i], convert to int and process as you want. 您可以通过origSev [i]访问单个数字,转换为int并根据需要进行处理。

Thanks!! 谢谢!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM