AttributeError: 'str' object has no attribute 'append'

前端 未结 5 688
夕颜
夕颜 2020-12-08 11:11
>>> myList[1]
\'from form\'
>>> myList[1].append(s)

Traceback (most recent call last):
  File \"\", line 1, in 
          


        
5条回答
  •  [愿得一人]
    2020-12-08 11:34

    myList[1] is an element of myList and it's type is string.

    myList[1] is str, you can not append to it. myList is a list, you should have been appending to it.

    >>> myList = [1, 'from form', [1,2]]
    >>> myList[1]
    'from form'
    >>> myList[2]
    [1, 2]
    >>> myList[2].append('t')
    >>> myList
    [1, 'from form', [1, 2, 't']]
    >>> myList[1].append('t')
    Traceback (most recent call last):
      File "", line 1, in 
    AttributeError: 'str' object has no attribute 'append'
    >>> 
    

提交回复
热议问题