Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将校验位附加到数字_Python - Fatal编程技术网

Python 如何将校验位附加到数字

Python 如何将校验位附加到数字,python,Python,好的,那么我要做的就是在用户输入的数字的末尾附加一个校验位 这是代码。稍后我会解释 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

好的,那么我要做的就是在用户输入的数字的末尾附加一个校验位 这是代码。稍后我会解释

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
但它不起作用

它给了我这个错误:

    number1.append(checkdigit)
 AttributeError: 'str' object has no attribute 'append'
根据我的经验,我只能猜测这意味着我不能附加字符串? 有没有关于如何将支票数字附加到支票末尾的想法或建议
用户输入的号码

append
用于数组。 如果要连接字符串,请尝试仅使用
+

>>> '1234' + '4'
'12344'

不能在Python中修改字符串。因此,没有append方法。但是,您可以创建一个新字符串,并将其分配给变量:

>>> s = "asds"
>>> s+="34"; s
'asds34'

Python中有两种类型的对象,可变和不可变。可变对象是那些可以就地修改的对象,正如名称所示,不可变对象不能就地更改,但每次更新都需要创建一个新字符串

因此,字符串对象没有append方法,这意味着需要在适当的位置修改字符串

所以你需要改变路线

number1.append(checkdigit)

注意


虽然后来的sytax看起来像是一个内置的附加,但在本例中它是对
number1=number1+checkdigit
的替换,它最终创建了一个新的不可变字符串

可能是重复的谢谢。虽然这是一个如此明显的方法,但我并没有想到。这说明我缺乏经验。最后,我只是使用了print(数字,+校验位),这就是你说的。没问题,网站是来帮助你的。请随意对提供的答案进行投票。我的建议是谷歌/阅读更多的文档,并在小代码片段上使用python解释器。感谢您花时间解释这一点-我非常感谢。我明白你的意思。最后,我使用了print(数字+校验位),这很有效。
number1 += checkdigit