TypeError: 'str' object does not support item assignment 的解决方法
2017-10-22 19:26
1306 查看
最近在写高精度减法时,遇到如下的错误提示:
TypeError: ‘str’ object does not support item assignment
在程序中,当出现借位情况时
由于n1是字符串,不是数组或列表,因此对字符串进行修改时提示该错误。
改正方法是将n1=n1[::-1]改为
n11=[int(x) for x in n1[::-1]] 转换为列表即可完成。
总结:字符串是不可变对象,不要试图用下标的方法去改变字符串的值。
TypeError: ‘str’ object does not support item assignment
在程序中,当出现借位情况时
if a<b: n1[i+1]=n1[i+1]-1 n1[i]=n1[i]+10
由于n1是字符串,不是数组或列表,因此对字符串进行修改时提示该错误。
改正方法是将n1=n1[::-1]改为
n11=[int(x) for x in n1[::-1]] 转换为列表即可完成。
总结:字符串是不可变对象,不要试图用下标的方法去改变字符串的值。
def sub(n11,n21): fh='' if int(n11)<int(n21): n11,n21=n21,n11 fh='-' if len(n11)>len(n21): n21='0'*(len(n11)-len(n21))+n21 print(str(n11)) print(str(n21)) n1=n11[::-1]] n2= n21[::-1] i=0 r=[] while (i<len(n1)): if int(n1[i])<int(n2[i]): t1=int(n1[i+1]) **n1[i+1]=str(t1-1)** n1[i]=str(int(n1[i])+10) r.append(int(n1[i])-int(n2[i])) i+=1 return r
相关文章推荐
- python 报错TypeError: 'range' object does not support item assignment,解决方法
- theano 中出现 TypeError: 'TensorVariable' object does not support item assignment
- TypeError: 'ItemMeta' object does not support item assignment
- Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法
- Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法
- Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法
- Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法
- Python List find方法报错 TypeError: 'str' does not support the buffer interface
- Python 3中套接字编程中遇到TypeError: 'str' does not support the buffer interface的解决办法
- error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 解决方法
- Error:The type or namespace name 'Office' does not exist in the namespace 'Microsoft' 解决方法
- sqoop导数类型不支持解决方法:Hive does not support the SQL type for column
- TypeError: 'dict_keys' object does not support indexing
- MCC出现错误这样的 ERROR 1251: Client does not support authentication pro 解决方法
- TypeError: 'str' does not support the buffer interface
- Strange SQLAlchemy error message: TypeError: 'dict' object does not support indexing
- error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 解决方法
- python socket send 错误:TypeError: 'str' does not support the buffer interface
- TypeError: 'str' does not support the buffer interface
- Python+client_connection.sendall(http_resp)+TypeError: 'str' does not support the buffer interface