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

**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 wont need a check digit to know the entry is invalid if its 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. Lets do an example, lets 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:\Users\kbingham\Documents\PythonProjects\CS101\Programming\Ideas\CheckDigits\AssignmentCheckDigits.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 ==>

>>>

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!