-1

The code does accept numbers but when entered wrong twice it comes up with an error

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=int(input('Please enter the 10 digit number: '))
       continue

else:
    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)
1
  • Btw, the else: after the while is not needed. You could take all that code and put indent it one block to the left. Also, I don't think you're doing the right thing with total = int(isbn[i])... Commented Feb 1, 2014 at 11:35

4 Answers 4

3

string has an isdigit method:

>>> "1231asd".isdigit()
False
>>> "123131241".isdigit()
True

Returns True only if all the characters in the string are digits.

So, your condition could be: not (len(isbn) == 10 and isbn.isdigit())

Edit: answering your new question (PLEASE, don't modify questions like that...)

Don't use input here, but raw_input. input will turn the number into an integer straight away. You want to keep the string, as you are going to operate on individual digits.

isbn= raw_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=raw_input('Please enter the 10 digit number: ')

Also, you don't need the continue; you only need to use continue if you want to skip part of a while/for loop.

Sign up to request clarification or add additional context in comments.

11 Comments

how can I add that in my code and where
Errr... the condition I suggested is for your while (I thought it was obvious enough)
Note that this is strictly for digits; if you want to confirm that the user has entered, say, a valid integer, you'll need to handle inputs like -10. For ISBNs, pure digits should do.
@user2357112 as I said "only if all the characters in the string are digits. OP wants to get an ISBN number, which consists only of digits, so...
so i will replace the while loop with
|
1

You have two options here:

  1. checking that the input string is 10 character long and that it contains only numbers:

    isbn = raw_input("Enter ISBN:")
    if isbn.isdigit() and len(isbn) == 10:
        # go on...
    
  2. using a regex to validate the input. This approach is much more flexible, but will cost you a little extra effort in understanding regex syntax:

    import re
    
    isbncheck = re.compile(r"^\d{10}$")
    
    isbn = raw_input("Enter ISBN:")
    if isbncheck.match(isbn):
        # go on...
    

Here "^\d{10}$" means: "a string composed only and exactly by 10 digits (shortcut for a digit: \d)".

4 Comments

isbn= raw_input('Please enter the 10 digit number: ') if isbn.isdigit() and len(isbn)==10: print('Please make sure you have entered a number which is exactly 10 characters long.') isbn=int(input('Please enter the 10 digit number: ')) continue else: 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)
No, you have to swap the "then" and the "else" blocks. Also, the continue in the then block is pointless.
can you please add that to my code because each time i do it comes up with an error
it doesn't matter ive done it but thanks a lot
0

I think this will be the simplest solution:

def MyFunc():
    isbn=raw_input('Please enter the 10 digit number: ')

    try:
        int(isbn)  # we try to get an int from the input... 

    except:
        print "Try again please."  # ... if we fail - ask user to start all over again
        MyFunc()

    """

    Your code goes here

    """

    return ISBN_number

MyFunc()

2 Comments

Maybe the try/except should only enclose the int conversion and only except a ValueError. The function should return something in case it is needed later.
Yeah, this would be better.
0

This is the answer to this question.It might have a few indentation errors but im sure you can sort it out after all it doesnt require much coding skills. I have just started this website today and hoping the user an vote this answer up thanks *************CODING*******************************

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: ')


else:
     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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.