Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

**PYTHON** Check Digits Check digits are used to validate a value, whether its a

ID: 3757258 • Letter: #

Question

**PYTHON**

Check Digits

Check digits are used to validate a value, whether its a Vehicle Identification Number (VIN), ISBN # used for books. The basic idea is that digit in a string is used to validate the other characters in the string. If one of the characters is entered incorrectly or transposed, then the calculated digit will not match the entered digit, and we can warn the user that the value is incorrect.

We are creating a check digit scheme for making unique assignment numbers for classes. The assignment number will be 13 characters in length. The last character is will be the check digit. The valid digits that can be used in our scheme are [0-9] and [A-F]. We won’t need a check digit to know the entry is invalid if it’s not 13 characters or uses a digit other than given.

Calculating the Check Digit

Each character has a numeric value. If the character is [0-9] then it has the corresponding value. A zero has a value of 0, a 1 has a value 1, and so on. A has a numeric value 10, B has a numeric value 11, C has a numeric value 12, etc on up to F which has a value of 15. We multiply each value by the weight of its position, and sum up the values.   Let’s do an example, let’s say our assignment is BAF189D234EA2.

Character

B

A

F

1

8

9

D

2

3

4

E

A

2

Value

11

10

15

1

8

9

13

2

3

4

14

10

Weight

0

1

2

3

4

5

6

7

8

9

10

11

0

Product

0

10

30

3

32

45

78

14

24

36

140

110

The sum of the product is 522. To calculate the result we use modulus 10 on the result. 522 mod 10 is 2, which is our check digit above.   If the assignment was entered as BAF189D234EA5, then the calculated checkdigit 2 does not match the check digit 5 as given. So we know the user entered an invalid value.

Class and assignment Type

The first character ( not index ), tells us what class the assignment is for. The only valid values for the first character is A, B, C, or D

First character in string

Class

A

CS101

B

CS191

C

CS201

D

CS291

Also, the second character lets us know the assignment type. The only valid value for the second character is A, B, C, D, or E.

Second character in string

Class

A

Test

B

Program

C

Quiz

D

Final

E

Other

Requirements

Validate the input is valid ( must be 13 characters, valid characters for all characters and limited further for character 1 and 2.

Once the user enters an empty string the program will end.

Development notes.

Using .upper() will change all the characters to the given case. Making comparisons easier. It is ok for the user to enter it in lower case. We will upper case the value for them.

Break the program down into meaningful functions. Try to solve one problem at a time.

Test Values to help in your testing

Value

Result

ABC

Incorrect, the value has to be 13 characters in length

BAF189D23ZEA2

Incorrect, there is an illegal character Z in the value

FAF189D234EA2

Incorrect, the first character cannot be F, only A-D

B8F189D234EA2

Incorrect, the second character cannot be 8, only A-E

BAF189D234EA2

Valid, class is CS191 and assignment is a Test

DE2017F000018

Valid, class is CS291 and the assignment is Other

AD1702FAC1990

Valid, class is CS101 and the assignment is a Final

Example

Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32

Type "copyright", "credits" or "license()" for more information.

>>>

RESTART: C:UserskbinghamDocumentsPythonProjectsCS101ProgrammingIdeasCheckDigitsAssignmentCheckDigits.py

Welcome to the assignment # validator

Enter the assignment number ==> ABC

The value entered was incorrect

Assignment # must be 13 characters in length

Welcome to the assignment # validator

Enter the assignment number ==> A000000000000000

The value entered was incorrect

Assignment # must be 13 characters in length

Welcome to the assignment # validator

Enter the assignment number ==> BAF189D23ZEA2

The value entered was incorrect

String contains invalid characters Z

Welcome to the assignment # validator

Enter the assignment number ==> FAF189D234EA2

The value entered was incorrect

The first digit must be a valid class identifier. ABCD

Welcome to the assignment # validator

Enter the assignment number ==> B8F189D234EA2

The value entered was incorrect

The second digit must be a valid assignment type identifier. ABCDE

Welcome to the assignment # validator

Enter the assignment number ==> BAF189D234EA2

The value given was valid

Assignment BAF189D234EA2 is for class CS191, and is a Test

Welcome to the assignment # validator

Enter the assignment number ==>

>>>

Character

B

A

F

1

8

9

D

2

3

4

E

A

2

Value

11

10

15

1

8

9

13

2

3

4

14

10

Weight

0

1

2

3

4

5

6

7

8

9

10

11

0

Product

0

10

30

3

32

45

78

14

24

36

140

110

Explanation / Answer

ScreenShot

-----------------------------------------------------

Program

#Check the length of given assignment number
def lengthFinding(str):
    if(len(str)!=13):
        return False
    else:
        return True
#Check the entered assignment number is valid letters
def charCheck(str):
    for x in str:
        if(x!='0'and x!='1'and x!='2'and x!='3'and x!='4'and x!='5'and x!='6'and x!='7'and x!='8'and x!='9'and x!='A'and x!='B'and x!='C'and x!='D'and x!='E'and x!='F'):
            print("The value entered was incorrect String contains invalid characters "+x)
            return False
    return True
#Check first letter validity
def firstCharCheck(str):
    if(str[0]!='A'and str[0]!='B'and str[0]!='C'and str[0]!='D'):
        return False
    else:
        return True
#Check second letter validity
def secondCharCheck(str):
    if(str[1]!='A'and str[1]!='B'and str[1]!='C'and str[1]!='D'and str[1]!='E'):
        return False
    else:
        return True     

#prompt for input
printStr="Assignment "
print("Welcome to the assignment # validator")
assignment_number=input("Enter the assignment number ==>")
#Convert to uppercase
assignment_number=assignment_number.upper();
#Check if the entered value null then exit otherwise execute
while(assignment_number!=""):
    #Length check
    if(lengthFinding(assignment_number)==True):
        #Each letter check
        if(charCheck(assignment_number)==True):
            #First digit check
            if(firstCharCheck(assignment_number)==True):
                #Second digit check
                if(secondCharCheck(assignment_number)==True):
                        #Is everything ok then valid print
                        print("The value given was valid")
                        printStr=printStr+assignment_number+"is for class "
                        if(assignment_number[0]=='A'):
                            printStr=printStr+"CS101"
                        elif(assignment_number[0]=='B'):
                            printStr=printStr+"CS191"
                        elif(assignment_number[0]=='C'):
                            printStr=printStr+"CS201"
                        elif(assignment_number[0]=='D'):
                            printStr=printStr+"CS291"
                        printStr=printStr+",and is a "
                        if(assignment_number[1]=='A'):
                            printStr=printStr+"Test"
                        elif(assignment_number[1]=='B'):
                            printStr=printStr+"Program"
                        elif(assignment_number[1]=='C'):
                            printStr=printStr+"Quiz"
                        elif(assignment_number[1]=='D'):
                            printStr=printStr+"Final"
                        elif(assignment_number[1]=='E'):
                            printStr=printStr+"Other"
                        print(printStr)
                #Else error print
                else:
                        print("The value entered was incorrect The second digit must be a valid assignment type identifier. ABCDE")
            else:
                    print("The value entered was incorrect The first digit must be a valid class identifier. ABCD")
         
    else:
        print("The value entered was incorrect Assignment # must be 13 characters in length")
    #Loop continuation check
    print("Welcome to the assignment # validator")
    assignment_number=input("Enter the assignment number ==>")
    assignment_number=assignment_number.upper();

-------------------------------------------------------------

Output

Welcome to the assignment # validator
Enter the assignment number ==>ABC
The value entered was incorrect
Assignment # must be 13 characters in length
Welcome to the assignment # validator
Enter the assignment number ==> A000000000000000
The value entered was incorrect
Assignment # must be 13 characters in length
Welcome to the assignment # validator
Enter the assignment number ==>BAF189D23ZEA2
The value entered was incorrect
String contains invalid characters Z
Welcome to the assignment # validator
Enter the assignment number ==>FAF189D234EA2
The value entered was incorrect
The first digit must be a valid class identifier. ABCD
Welcome to the assignment # validator
Enter the assignment number ==>B8F189D234EA2
The value entered was incorrect
The second digit must be a valid assignment type identifier. ABCDE
Welcome to the assignment # validator
Enter the assignment number ==>BAF189D234EA2
The value given was valid
Assignment BAF189D234EA2is for class CS191,and is a Test
Welcome to the assignment # validator
Enter the assignment number ==>
Press any key to continue . . .