Project 2: ISBN Check

Every book published is assigned a code called an ISBN, for International Standard Book Number. ISBNs before 2007 were 10 digits long, while new books are now assigned 13-digit ISBNs. However, not any arbitrary sequence of 10 or 13 digits can be an ISBN. Because when people have to type these numbers into computers it is easy to accidentally transpose two digits, the last digit of every ISBN serves as something called a check digit; it can detect in many cases whether the previous digits were entered correctly!

The way the check digit works (for a 10-digit ISBN) is that its value is calculated from the previous nine digits as follows. Assuming the first 9 digits of the ISBN are called d1 through d9, one first calculates the following value "s:"

s = (1 * d1) + (2 * d2) + ... + (8 * d8) + (9 * d9)

The check digit is always the remainder of dividing s by 11, except when the remainder is 10, the check digit is "X" instead (since using "10" would create an 11-digit ISBN).

You will write a computer program to calculate the check digit of any ISBN given the first nine digits.

What you need to do

You must write a program that will prompt the user to enter the first nine digits, one at a time, of a 10-digit ISBN. After all the digits have been entered, your program should calculate the check digit and display it.

When entering digits one at a time, the user should be prevented from entering a number less than 0 or greater than 9. You may assume they won't enter an arbitrary string or a floating point number, but if they enter an integer that falls outside of the range 0--9, then print an error message and ask them to re-enter that digit. Don't make them start over from the beginning of the number, however; just have them re-enter the digit they messed up on.

You may design your program in any way you see fit, though you should follow the guidelines for good programming that we've discussed in class: using comments, choosing appropriate variable names, making use of functions where appropriate, etc.

Sample Interaction

Digit? 1
Digit? 5
Digit? 8
Digit? 2
Digit? 6
Digit? 1
Digit? 6
Digit? 8
Digit? 4
Last digit of ISBN should be 1.
Another run:
Digit? 0
Digit? 6
Digit? 8
Digit? 8
Digit? -1
Bad digit, re-enter please.
Digit? 0
Digit? 6
Digit? 6
Digit? 7
Digit? 8
Last digit of ISBN should be X.
Your code does not have to follow this exact script verbatim, but all the mentioned functionality should be there: asking for digits, printing the check digit, and not allowing digits out of range to be entered.

Hints

Use the procedure we discussed in class for writing programs: before even thinking about code, write down the algorithm in pseudocode, and decide on what variables you will need, what their names will be, what their data types will be, and most importantly, what each variable represents in the program.

What to turn in

Through Moodle, turn in your code as a file called isbn_yourLastName_yourFirstName.py.