-4
def isbn10(x):
    y = [int(i) for i in x]
    a = y[0] * 10 + y[1] * 9 + y[2] * 8 + y[3] * 7 + y[4] * 6 + y[5] * 5 + y[6] * 4 + y[7] * 3 + y[8] * 2
    checkno = a % 11
    checkno = 11 - checkno
    if checkno == 10:
        checkno = "X"
    if checkno == prompt[9]:
        print("Your ISBN10 number is correct.")
    else:
        print("Your check number is wrong. It should be " + prompt[0:9] + checkno)


def isbn13(x):
    y = [int(i) for i in x]
    even = y[1] + y[3] + y[5] + y[7] + y[9] + y[11]
    odd = y[0] + y[2] + y[4] + y[6] + y[8] + y[10]
    even = even * 3
    total = even + odd
    checksum = total % 10
    checksum = 10 - checkno
    if checksum == prompt[-1]:
        print("Your ISBN13 number is correct.")
    else:
        print("Your check number is wrong. It should be " + prompt[0:-2] + checkno)
    #ok...


def main():
    prompt = input("Please type in your ISBN number.\n")
    prompt = str(prompt)

    if len(prompt) == 10:
        isbn10(prompt)
    elif len(prompt) == 13:
        isbn13(prompt)
    else:
        print("Your ISBN number is invalid")


while True:
    main()
    if input('Continue? [y/n]') == 'n':
        break

When I run the program...:

Please type in your ISBN number. 9876543210

Traceback (most recent call last):
  File "C:\Users\yc\Desktop\Computing\computing\python\Python ISBN\isbn_checker.py", line 29, in <module>
    isbn10(prompt)
  File "C:\Users\yc\Desktop\Computing\computing\python\Python ISBN\isbn_checker.py", line 11, in isbn10
    print("Your check number is wrong. It should be " + prompt[0:9] + checkno)
TypeError: cannot concatenate 'str' and 'int' objects
Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
Pythoner
  • 11
  • 2
  • The error is self-explanatory. You're trying to concatenate a string and integer object. (Use string formatting) – Ashwini Chaudhary Sep 01 '13 at 12:08
  • You are mixing numbers and strings in the same `checkno` variable. It is never healthy to do that, even in dynamic languages like Python. – Frédéric Hamidi Sep 01 '13 at 12:08
  • This question appears to be off-topic because it is asking for a codereview; [codereview.stackexchange.com](http://codereview.stackexchange.com) –  Sep 01 '13 at 12:09
  • 2
    Actually lots of things are wrong with your code. The fact that you don't follow [PEP-8](http://www.python.org/dev/peps/pep-0008/) is one of them. And those `even` and `odd` lists.. seriously? You can build them in a [much nicer way](http://stackoverflow.com/questions/4988002/shortest-way-to-slice-even-odd-lines-from-a-python-array)! Also, `y=[int(i) for i in x]` can be written `y = map(int, x)` – ThiefMaster Sep 01 '13 at 12:09
  • Please learn to format your code so that it would be readable to you and others, or at least do it before sharing your code on the internet. Here's something to get you started: http://www.python.org/dev/peps/pep-0008/. I'll clean up the code in your question for now. – Erik Kaplun Sep 01 '13 at 13:45
  • P.S. You should be using `raw_input()` instead of `input()`; this will always give you a string like you want. – Erik Kaplun Sep 01 '13 at 13:52
  • This question appears to be off-topic because it is about a very frequent beginner mistake, concatenating strings and numbers. It is too localized – joaquin Sep 04 '13 at 16:20
  • why raw_input since im using Python3.3? – Pythoner Sep 06 '13 at 03:34

2 Answers2

1

checkno is an integer in this case, and you are trying to concatenate it with a string.

Replace checkno with str(checkno):

print("Your check number is wrong. It should be " + prompt[0:9] + str(checkno))

or, better use format() instead of concatenation:

print("Your check number is wrong. It should be {}{}".format(prompt[0:9], checkno))

Also:

  • checkno variable is not defined in isbn13() function
  • there is no main() function in the program
  • the code is difficult to read and understand. One of the reason is that it doesn't follow PEP-8 style at all
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Or, if they're using Python3, then `print("Your check number is wrong. It should be", prompt[0:9], checkno)` or otherwise use suitable string formatting `.format` instead of concatenation and save mucking about with explicit conversions... but yeah... lots of things wrong with the OP's code ;) – Jon Clements Sep 01 '13 at 12:10
  • 1
    `checkno` is only an integer *sometimes*, see `checkno = "X"` – Frédéric Hamidi Sep 01 '13 at 12:10
  • `str()` or `{}` with `.format()` works fine in any case though – ThiefMaster Sep 01 '13 at 12:10
0
print("Your check number is wrong. It should be " + prompt[0:9] + checkno)
TypeError: cannot concatenate 'str' and 'int' objects

Is actually pretty self explanatory, did you even read the error message?

First never store different types of information in the same variable.

Avoid concatenate with the + in Python use the build in formatting.

print("Your check number is wrong. It should be %s%d" % (prompt[0:9],checkno) )