所以我要做的是在用户输入的数字的末尾附加一个校验位 这是密码。以后我再解释。在
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
但没用
它给了我一个错误:
^{pr2}$从我的经验不足,我只能猜测这意味着我不能追加一个字符串? 有什么想法或建议,我可以如何把校验位追加到 用户输入的号码?在
不能在Python中修改字符串。因此没有append方法。但是,您可以创建一个新字符串并将其分配给变量:
append
表示数组。 如果要连接字符串,请尝试使用+
。在Python中有两种类型的对象,可变的和不可变的。正如你所建议的那样,每个对象都可以在可变的地方被修改,但是你不能在一个新的地方修改对象的名字。在
因此,string对象没有append方法,这意味着需要就地修改字符串。在
所以你需要改变路线
到
^{pr2}$注意
虽然后面的sytax看起来像是一个inplace append,但是在本例中它是
number1 = number1 + checkdigit
的替代品,它最终创建了一个新的不可变字符串。在相关问题 更多 >
编程相关推荐