Question: In Python: Checksum Validation Fixed-length numbers are commonly used for identification purposes; for example, think about bank account numbers and social security numbers. Because they
In Python: Checksum Validation
Fixed-length numbers are commonly used for identification purposes; for example, think about bank account numbers and social security numbers. Because they are so often typed in by humans, their use is sometimes error-prone. One historical way of automated validity checking of such numbers is to reserve one digit as the "check" digit, whose value is determined by a special "checksum" algorithm applied to the other digits. Part (a) in the figure below illustrates how a trailing check digit is calculated for the number 176248: it is the digit that when appended to the number in the ones place produces an augmented number whose checksum is divisible by 10. Here, the checksum algorithm involves doubling each digit and adding the results together; in the case that doubling a digit produces a two-digit result, those digits are also added.
Part (b) demonstrates the validation procedure that just performs the checksum algorithm again on the augmented number. Note that it is assumed by the checksum algorithm that the trailing (or ones) digit is the check digit, so it is not doubled when added to the checksum. Note that if any one of the other digits is incorrect, the checksum fails. Thus, the check digit "3" in the ones position "validates" the number 176248.
The code template below, my_checksum.py, partially defines a function do_checksum() with a single integer argument argument. The last digit of this argument is a check digit such that the checksum is expected to be divisible by 10. Your do_checksum() should return True if the number's checksum is divisible by 10, and False otherwise.
The __main__ block is provided to allow you to test the program on your local computer. It does not matter what you put in the __main__ block; only your do_checksum() function will be tested.
Can you please check what is wrong with my code? please note that i have to keep the bolded parts in the following
def do_checksum (N): passes_checksum = False a = list(input()) check_digit = a.pop() oth_digit = [] for index,digit in enumerate(a): if index % 2 != 0: d_digit = int(digit) * 2 if d_digit > 9: d_digit -= 9 oth_digit.append(d_digit) else: oth_digit.append(int(digit)) passes_checksum = int(check_digit)+sum(oth_digit) if passes_checksum % 10 == 0: passes_checksum = True return passes_checksum if __name__ == '__main__': a=728498273 print('{:d} {:s} the checksum test.'.format(a,'passes' if do_checksum(a) else 'does not pass')) a=728498271 print('{:d} {:s} the checksum test.'.format(a,'passes' if do_checksum(a) else 'does not pass'))
Check digit 3 1 7 6 N 4 8 3 4 16 1 1 + 1 + 4 4 + 6 + + 4 + 4 + 1 + + 6 + II 27 + 3 3 = 30 (a) Computing the check digit. 1 7 6 2 4 8 3 3 1 4 4 4 1 6 1 + 1 + 4 + 6 + 4 + 4 + 1 + 6 + + 3 3 = 30 (b) Checksum validation
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
