Part 2: Programming - 75% A program to compute the last digit of a barcode If you look on the back of the box/label of any commercial product, you will find a barcode which represents its product...


Part 2: Programming - 75%


A program to compute the last digit of a barcode


If you look on the back of the box/label of any commercial product, you will find a barcode which represents its product code. A product code is used to uniquely identify it (e.g. when you're at the grocery store, the cashier scans the barcode with his/her barcode reader and instantly knows which product you are buying and its price).





example barcode


Product codes vary in length from 8 to 18 digits. You may have heard of these product codes referred to as UPCs (Universal Product Codes), but UPCs are only one of the many flavours of product codes found on items we buy every day:

Different Product codes and their lengths




























Produce Code NameLength
EAN-88
UPC-A12
EAN-1313
EAN-1414
SSCC18

The last digit of every flavour of product code is a check digit used as a validity check. This lets the scanner check for errors in the scanning: if the check digit is right, it probably scanned correctly. The check digit is calculated by performing a calculation on the firstn-1 digits of an-digit product code.


Your programming task for this assignment will be to create a program which calculates the check digit for any of the different product codes listed above, given the firstn-1 digits of a product code. Your program should be created in a file calledupc.py.


The algorithm to implement check digit calculation for any length product code is the same. Let's assume the first 11 digits of a UPC-A code are “23139085377” and we would like to derive the check digit:



  1. From the right to the left, starting with position 1 every other position is odd and starting with 2, every other position is even:
















































    Position1234567891011
    Even/Oddoddevenoddevenoddevenoddevenoddevenodd
    Digit23139085377



  2. Add together all the digits in the odd positions and multiply the result by 3:

    (2 + 1 + 9 + 8 + 3 + 7) * 3 = 90




  3. Add together all the digits in the even positions:

    3 + 3 + 0 + 5 + 7 = 18




  4. Add together these two numbers:

    90 + 18 = 108




  5. Take the remainder of dividingthe negativeof this number by 10. (In Python the%operator gives the remainder.)

    -108 % 10 = 2





So, in the example, the check digit is 2. The whole UPC code for this product is "231390853772".


Using the above described algorithm, create a program that:



  1. Asks the user which type of product code he/she would like to find the check digit for.

  2. Based on the user's choice of product code, asks the user for the firstn-1 digits of the product code. [Get the input as a string; it's easier to work with the string, so don't convert to an integer.]

  3. Using the user's input of the firstn-1 digits, finds the check digit.

  4. Displays the resulting product code.


Your program should interact with the user as follows:

Check Digit Calculator
--------------------
1. EAN-8
2. UPC-A
3. EAN-13
4. EAN-14
5. SSCC
--------------------
Choose your product code type:2

Enter first 11 digits of your product code:23139085377

--------------------
The check digit is 2.
The product code is 231390853772.

Remember, your program should ask for the appropriate number of digits based on the type of product code for which a check digit is being calculated. For this, you'll need to useif/elseconditional statements and boolean expressions (Section 3.1 of the Study Guide). Your program should also catch the following two error conditions by printing out the appropriate message, and exiting the program:



  • The user enters a number that is not on the menu:Check Digit Calculator
    --------------------
    1. EAN-8
    2. UPC-A
    3. EAN-13
    4. EAN-14
    5. SSCC
    --------------------
    Choose your product code type:18

    Error: 18 is an invalid menu choice.


  • The user enters a number of digits not expected by the program based on the type of product code selected:Check Digit Calculator
    --------------------
    1. EAN-8
    2. UPC-A
    3. EAN-13
    4. EAN-14
    5. SSCC
    --------------------
    Choose your product code type:1

    Enter first 7 digits of your product code:12

    Error: You should have entered 7 digits, but you entered 2 digits.



Before you submit the program, check it with some products you have around. Determine the type of barcode from the number of digits; enter all but the last digit and make sure the check digit matches. (But see the last “hint” below.)


Hints




  • To create this program, you will need the following aspects of Python that you might not have seen yet:



    • String indexing. If you have a string variablestr, you can get at the first character withstr[0], the second withstr[1], and so on.

    • Thelenfunction that returns the length of a string.



  • You will need to convert individual digits to integers to do the arithmetic. For example, if we had a product code withn=5, you might do something like this:oddplaces = int(code[0]) + int(code[2]) + int(code[4])

  • Write and test small parts of the program at a time. You could start by getting EAN-8 working, then add the menu, then add the other lengths, and add the error-checking last. Test each part as you go and make sure it's working before you move on. Doing this will let you concentrate on smaller, more manageable problems.

  • You can use a loop to do the calculation across the digits. This can make your code simpler overall, but if you're not comfortable with loops, you can write a long expression for the sum of the odd and even digits.

  • Try to use good variable names and keep your code as readable as possible. Coding style is part of the marking scheme.


When you're done, create a ZIP file containing all of the files you created for this assignment and submit it.
To create some test cases, use:
https://www.axicon.com/checkdigitcalculator.html

Oct 20, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here