0

Ok so what I am trying to do is append a check digit to the end of a number that the user has entered This is the code. I will explain afterwards.

isbn_list = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
isbn = [0,1,2,3,4,5,6,7,8,9]
isbnMult = [11,10,9,8,7,6,5,4,3,2,1]

number = input("Input ISBN number: ")
isbnnumber= 0

for i in range(len(number)):
    found= False
    count= 0
    while not found:
        if number[i] == isbn_list[count]:
           found= True
           isbnnumber= isbnnumber + isbn[count] * isbnMult[i]
        else:
           count += 1

total=isbnnumber%11
checkdigit=11-total
check_digit=str(checkdigit)  #I know I have to append a number to a string
number.append(checkdigit)   #so i thought that I would make the number into a 
print(number)               #string and then use the '.append' function to add
                            #it to the end of the 10 digit number that the user enters

but it doesn't work

It gives me this error:

    number1.append(checkdigit)
 AttributeError: 'str' object has no attribute 'append'

From my inexperience, I can only guess that that means that I cannot append a string? Any idea or suggestions on how I might go about appending the check digit to the end of the number entered by the user?

  • possible duplicate of [Good way to append to a string](http://stackoverflow.com/questions/4435169/good-way-to-append-to-a-string) – hivert Feb 02 '14 at 18:37

3 Answers3

1

You can't modify a string in Python. Therefore there is no append method. You can however create an new string and assign it to the variable:

>>> s = "asds"
>>> s+="34"; s
'asds34'
hivert
  • 10,579
  • 3
  • 31
  • 56
0

append is for array. try to just use + if you want to concatenate strings.

>>> '1234' + '4'
'12344'
Jeremy D
  • 4,787
  • 1
  • 31
  • 38
  • Thank you. As much as this is such an obvious method it did not occur to me. This goes to show my lack of experience. In the end I just used print(number, + checkdigit) which is what you were saying. – useranonymous Feb 02 '14 at 20:33
  • No problem, the website is here to help. Feel free to upvote answers provided. My advice would be to google/read documentation a bit more, and also play with the python interpreter on small snippets of code. – Jeremy D Feb 02 '14 at 21:57
0

There are two types of objects in Python, mutable and immutable. Mutable objects are those, which can be modified in-place, where as immutable objects as the name suggests cannot be changed in-place but for every update, you need to create a new string.

So, string objects do not have an append method, which would mean, would need to modify the string in-place.

So you need to change the line

number1.append(checkdigit)

to

number1 += checkdigit

Note

Though the later sytax seems like an inplace append, yet in this case its a replacement for number1 = number1 + checkdigit, which ends up creating a new immutable string.

Abhijit
  • 62,056
  • 18
  • 131
  • 204
  • Thank you for taking the time to explain this- I appreciate it. I understand what you mean. In the end I just used print(number, + checkdigit) which worked. – useranonymous Feb 02 '14 at 20:36