Python 3.x 有人能逐行检查一下这个代码并用伪代码或英语解释一下吗

Python 3.x 有人能逐行检查一下这个代码并用伪代码或英语解释一下吗,python-3.x,Python 3.x,我的任务是“根据七位数计算GTIN-8产品代码” 我需要逐行解释上述代码。通过键入help(enumerate) 枚举(iterable[,start])->索引的迭代器,iterable的值 返回枚举对象。iterable必须是另一个支持迭代的对象。枚举对象生成包含计数(从开始,默认为零)和iterable参数生成的值的对。枚举对于获取索引列表非常有用: %运算符返回x/y的剩余部分 while True: try: number = input('Enter') #Asks user

我的任务是“根据七位数计算GTIN-8产品代码”
我需要逐行解释上述代码。

通过键入
help(enumerate)

枚举(iterable[,start])->索引的迭代器,iterable的值

返回枚举对象。iterable必须是另一个支持迭代的对象。枚举对象生成包含计数(从开始,默认为零)和iterable参数生成的值的对。枚举对于获取索引列表非常有用:

%
运算符返回
x/y的剩余部分

while True:
try:
    number = input('Enter') #Asks user to input 7 digit number

    if len(str(number)) != 7:
        print('Incorrect')
    if len(str(number)) == 7:
        print('Okay')
        multiplier = [3,1]
        times = ''
        total = 0
        for index, digit in enumerate(list(str(number))):
            total = total + int(digit)*multiplier[index%2]
            times = times+str(int(digit)*multiplier[index%2])+', '
        mof10 = total + (10 - total%10)
        checkdigit = mof10 - total
        final = str(number) + str(checkdigit)
        print (times[:-1])
        print(total)
        print(mof10)
        print(checkdigit)
        print(final)
        break
except ValueError:
    print('Not a number')
下面是用注释解释的代码

10 / 5 = 2 r 0  |  10 % 5 = 0
7  / 2 = 3 r 1  |  7 % 2 = 1

你自己采取了什么措施来解决这个问题?我将试图结束这个问题,因为它太宽泛了。太完美了,正是我所需要的。谢谢你的欢迎。我没有在回答中说明,但是您会发现一些
str
转换有时是毫无意义的,因为
input
中的值已经是字符串了。此外,您会发现,理解某些代码(和调试)的最佳方法是大量打印,以查看发生了什么事。Brilliant,谢谢,我仔细阅读并修改了您的评论,只是为了让经验较少的程序员更容易理解。谢谢你的提示,我相信这会很有帮助的!:)编辑P.S.并不是说你的评论有什么问题,只是想用我自己的话来说,因为我认为这会帮助我更好地记住。
while True: # Loop will continue until break is called
    try:
        number = input('Enter') #Asks user to input 7 digit number

        # Checks if the length of the input
            # If not 7 characters long
            # Will then ask for input again due to while loop
        if len(str(number)) != 7:
            print('Incorrect')

            # If is 7 chacters long
        if len(str(number)) == 7:
            print('Okay')
            multiplier = [3,1]
            times = ''
            total = 0
            # example input for number '1234567'
            # list(str(number))
            # '1234567' -> ['1', '2', '3', '4', '5', '6', '7']
            for index, digit in enumerate(list(str(number))):
                # index%2 returns either 0 or 1
                # this is used to get 3 or 1 from multiplier
                # depending on if the index is odd or even

                # for first index and digit from example
                # index = 0, digit = '1'
                # total = total + 1 * 3
                total = total + int(digit)*multiplier[index%2]

                # adds the multiplier value from digit * multiplier[index%2]
                # to times as a string. First value is
                # times = times + str(1 * 3) which is 3
                times = times+str(int(digit)*multiplier[index%2])+', '
            # essentially rounds up to nearest 10
            mof10 = total + (10 - total%10)
            # gets the number it was rounded up by
            checkdigit = mof10 - total
            final = str(number) + str(checkdigit)
            print(times[:-1]) # slice the string to return everything except the last value
            print(total)
            print(mof10)
            print(checkdigit)
            print(final)
            break # exit while loop
        # If the convertion from string to int returns an error
        # meaning a non-number was entered into input eg. !@#$
        # print the following, while loop will continue and prompt again
    except ValueError:
        print('Not a number')