[Solved] Could someone please go through this code line by line and explain it in Pseudocode or English


Some info on enumerate by typing help(enumerate)

enumerate(iterable[, start]) -> iterator for index, value of iterable

Return an enumerate object. iterable must be another object that supports iteration. The enumerate object yields pairs containing a count (from start, which defaults to zero) and a value yielded by the iterable argument. enumerate is useful for obtaining an indexed list:

The % operator returns the remainder of x / y eg

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

Here is the code explained with comments

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

3

solved Could someone please go through this code line by line and explain it in Pseudocode or English