Question

mainmenu = input("Welcome to my ISBN calculator, please select an option\n\
1. Load ISBN Calculator\n\
2. Exit Program\n\
") 


(mainmenu) 

if mainmenu == ("2"):
    print ("The ISBN Calculator will now close, thank you for using!")
    time.sleep(1.5) 
    exit()


elif mainmenu == ("1"):
    ISBN = input(" Please enter the 10 digit number exactly\n\
") 


    Digit1 = int(ISBN[0])*11
    Digit2 = int(ISBN[1])*10
    Digit3 = int(ISBN[2])*9
    Digit4 = int(ISBN[3])*8
    Digit5 = int(ISBN[4])*7
    Digit6 = int(ISBN[5])*6
    Digit7 = int(ISBN[6])*5
    Digit8 = int(ISBN[7])*4
    Digit9 = int(ISBN[8])*3
    Digit10 = int(ISBN[9])*2

sum=(Digit1+Digit2+Digit3+Digit4+Digit5+Digit6+Digit7+Digit8+Digit9+Digit10)

num=sum%11
Digit11=11-num 
if Digit11==10:
    Digit11='X'
    ISBNNumber=str(ISBN)+str(Digit11) 
    print('The ISBN number is -->    ' + ISBNNumber)

This is my code and it always comes up with the error of Digit 1 is not defined whenever I try enter my 10 digit number, any help?

Was it helpful?

Solution 2

It will be work. Indent is important.

mainmenu = input("Welcome to my ISBN calculator, please select an option\n\
1. Load ISBN Calculator\n\
2. Exit Program\n\
")

if mainmenu == "2":
    print ("The ISBN Calculator will now close, thank you for using!")
    time.sleep(1.5)
    exit()


elif mainmenu == "1":
    ISBN = input(" Please enter the 10 digit number exactly\n")
    Digit1 = int(ISBN[0])*11
    Digit2 = int(ISBN[1])*10
    Digit3 = int(ISBN[2])*9
    Digit4 = int(ISBN[3])*8
    Digit5 = int(ISBN[4])*7
    Digit6 = int(ISBN[5])*6
    Digit7 = int(ISBN[6])*5
    Digit8 = int(ISBN[7])*4
    Digit9 = int(ISBN[8])*3
    Digit10 = int(ISBN[9])*2

    sum=(Digit1+Digit2+Digit3+Digit4+Digit5+Digit6+Digit7+Digit8+Digit9+Digit10)

    num=sum%11
    Digit11=11-num
    if Digit11==10:
        Digit11='X'
        ISBNNumber=str(ISBN)+str(Digit11)
        print('The ISBN number is -->    ' + ISBNNumber)

Note. This code is just work code, not a good implementation.

OTHER TIPS

Why the line:

(mainmenu)

?

In your if statements remove the ():

if mainmenu == "1":
    ...
elif mainmenu == "2":
    ...
else:
    print("Invalid menu option")
    exit()

The problem occurs when you execute that code with Python 2. Use Python 3 instead.

In Python 2, input evaluates the input you provide, so if you enter 1, then mainmenu is 1 (the number) and not "1" (the string), thus both of your if-checks fail and your code arrives at the sum=... part without any ISBN number being inputted.

As commented above, your "ISBN" is quite different from the standard, which has either 10 or 13 digits including check digit.

A clean implementation for ISBN-10 calculation would be:

from string import digits

checkTemplate = digits + "X"

def isbn(isbnBody):
    """append check digit to a isbn given as string without check digit"""
    assert len(isbnBody) == 9
    s = sum([int(isbnChar)*multiplier for isbnChar, multiplier in zip(isbnBody, range(1,10))])
    checkDigit = checkTemplate[s % 11]
    return isbnBody + checkDigit
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top