Question

I have created a code with the help of a few user on stackoverflow. I have found a problem in my code. The problem with my code is that when the user enters the ISBN wrong more than twice then it code up with an error

here's my code:

isbn= input('Please enter the 10 digit number: ')
while not(len(isbn) == 10 and isbn.isdigit()):
    print('Please make sure you have entered a number which is exactly 10 characters long.')
    isbn=input('Please enter the 10 digit number: '))
    continue

else:
    total= 0
   for digit in isbn: total += int(digit)
    calculation=total%11
    digit11=11-calculation
    if digit11==10:
       digit11='X'
    iSBNNumber=str(isbn)+str(digit11)
    print('Your 11 digit ISBN Number is ' + iSBNNumber)
Was it helpful?

Solution

In a while loop, the following code tries to convert the input string to int.

isbn = int(input('Please enter the 10 digit number: '))

int objects has no isdigit methods; cause AttributeError.

>>> isbn = 12345
>>> isbn.isdigit()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'isdigit'

len(int) causes TypeError.

>>> len(isbn)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: object of type 'int' has no len()

Remove int(..) call.

isbn = input('Please enter the 10 digit number: ')

OTHER TIPS

Get rid of that int in the while loop. In fact, you want to convert ISBN to number only after checking that it is a number, indeed.

Also, you don't need neither the else nor the continue statements:

isbn= input('Please enter the 10 digit number: ')
while not(len(isbn) == 10 and isbn.isdigit()):
    print('Please make sure you have entered a number which is exactly 10 characters long.')
    isbn=input('Please enter the 10 digit number: ')

total= 0
for i in range(len(isbn)):
    total= int(isbn[i])
calculation=total%11
digit11=11-calculation
if digit11==10:
   digit11='X'
iSBNNumber=str(isbn)+str(digit11)
print('Your 11 digit ISBN Number is ' + iSBNNumber)

I don't know if the algorithm implemented is correct, but the code above will run.

This is the answer fully working

isbn= input('Please enter the 10 digit number: ')
while not(len(isbn) == 10 and isbn.isdigit()):
    print('Please make sure you have entered a number which is exactly 10 characters long.')
    isbn=input('Please enter the 10 digit number: ')

total= 0
for i in range(len(isbn)):
    total= int(isbn[i])
calculation=total%11
digit11=11-calculation
if digit11==10:
   digit11='X'
iSBNNumber=str(isbn)+str(digit11)
print('Your 11 digit ISBN Number is ' + iSBNNumber)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top