在python代码中替换数组值时出现问题
TypeError:“str”对象不支持项分配 我在Received_代码中输入了一个11位数的数字,稍后在获得错误位置后,我想根据条件将该数字替换为0或1在python代码中替换数组值时出现问题,python,Python,TypeError:“str”对象不支持项分配 我在Received_代码中输入了一个11位数的数字,稍后在获得错误位置后,我想根据条件将该数字替换为0或1 recieved_code = (input()) #Detecting the position of error parList = parList[: : -1] error = 0 for i in range(0,len(parList)): error = error + pow(2,i)*parList[i] pr
recieved_code = (input())
#Detecting the position of error
parList = parList[: : -1]
error = 0
for i in range(0,len(parList)):
error = error + pow(2,i)*parList[i]
print("\nError is at position:",error)
#Correcting the error
if recieved_code[error-1] == 1:
print("It's 1")
recieved_code[error-1] = 0
else:
print("It's 0")
recieved_code[error-1] = 1
print("Corrected code:",recieved_code)
我希望0被1替换,或者1被0替换
字符串是不可变的
这意味着一旦创建,就不能修改字符串本身。您需要在处理/连接/etc之后创建另一个字符串。Python字符串是不可变的。你能给我一个解决方案吗