Python 属性错误:';str';对象没有属性';插入';

Python 属性错误:';str';对象没有属性';插入';,python,Python,我正试图在我的链接列表中插入一段文本。然而,当我尝试这样做时,我会得到一个错误“AttributeError:'str'对象没有属性'insert'”。我正在用python 3.3编写这篇文章 class Node: def __init__(self, item= None , link= None ): self.item = item self.next = link def __str__(self): return str(self.item) class

我正试图在我的链接列表中插入一段文本。然而,当我尝试这样做时,我会得到一个错误“AttributeError:'str'对象没有属性'insert'”。我正在用python 3.3编写这篇文章

class Node:
def __init__(self, item= None , link= None ):
    self.item = item
    self.next = link

def __str__(self):
    return str(self.item)


class List:
def __init__(self):
    self.head = None
    self.count = 0

def is_empty(self):
    return self.count == 0

def is_full(self):
    return False

def reset(self):
    self.__init__()

def __len__(self):
    return self.count

def _getNode(self, index):
    node = self.head
    for _ in range(index):
        node = node.next
    return node

def insert(self, index, item):
    if index < 0:
        index = 0

    elif index > len(self):
        index = len(self)

    if index == 0:
        self.head = Node(item, self.head)
    else:
        node = self._getNode(index - 1)
        node.next = Node(item, node.next)

    self.count += 1

def delete(self, index):
    if self.is_empty():
        raise IndexError("List is empty")
    if index < 0 or index >= len(self):
        raise IndexError("Index is out of range")

    if index == 0:
        self.head = self.head.next
    else:
        node = self._getNode(index - 1)
        node.next = node.next.next

    self.count -= 1

import LinkedList
text= LinkedList.List()

def insert_num(line,text):
    text.insert(line - 1,text)

def delete_num(line):
    if line is None:
        text.reset
    else:
        text.delete(line)

def print_num(line):
    if line is None:
        i= 0
        while i< line.count:
            item= text._getNode(i)
            print (item)
    else:
        print(text._getNode(line))

while True:
    print("1. Insert")
    print("2. Delete")
    print("3. Print")
    print("4. Quit")
    command = int(input())
    if command == 1:
        line = int(input("At what line do you wish to insert your text?: "))
        text = input("Text: ")
        insert_num(line,text)
    elif command == 2:
        line = int(input("What line do you wish to delete?: "))
        delete_num(line)
    elif command == 3:
        line = int(input("What line do you wish to print?: "))
    elif command == 4:
        break
    else:
        print("invalid input, please select one of the following numbers:")
类节点:
定义初始化(self,item=None,link=None):
self.item=项目
self.next=链接
定义(自我):
返回str(self.item)
班级名单:
定义初始化(自):
self.head=无
self.count=0
def为空(自身):
返回self.count==0
def已满(自身):
返回错误
def重置(自):
self.\uuuu init\uuuuu()
定义(自我):
返回自计数
def_getNode(self,index):
node=self.head
对于范围内的uu(索引):
node=node.next
返回节点
def插入(自身、索引、项目):
如果指数<0:
索引=0
elif索引>透镜(自身):
索引=len(自)
如果索引==0:
self.head=节点(项目,self.head)
其他:
node=self.\u getNode(索引-1)
node.next=节点(项,node.next)
self.count+=1
def delete(自我,索引):
如果self.is_为空():
提升索引器(“列表为空”)
如果索引<0或索引>=len(自):
提升索引器(“索引超出范围”)
如果索引==0:
self.head=self.head.next
其他:
node=self.\u getNode(索引-1)
node.next=node.next.next
self.count-=1
导入链接列表
text=LinkedList.List()
def insert_num(行,文本):
文本。插入(第1行,文本)
def delete_num(行):
如果行为“无”:
text.reset
其他:
文本。删除(行)
def打印数量(行):
如果行为“无”:
i=0
而我
在主循环中,调用
insert_num(行,文本)
。但是
text
这是您在上面输入的文本字符串,而不是全局变量
text
,它是LinkedList类的一个实例。正如错误所说,字符串没有insert方法(因为它们是不可变的)。

您可以调用这两行

text = input("Text: ")
insert_num(line,text)
生成的
text
变量将是类型
str
,而不是链表。正如错误告诉您的那样,字符串没有插入的

当你拨打这两条电话时:

import LinkedList
text= LinkedList.List()

这是一个不同的
text
变量,与
insert\u num
函数范围内存在的变量不同。

问题在于变量范围。调用
insert_num()
过程时,您希望将新行(str类型的
text
参数)插入到
LinkedList
行中,该行也称为
text
,但由于该方法有一个同名参数,因此(链接列表)超出了范围,所以不能通过程序看到

text= LinkedList.List()

def insert_num(line,text):
    text.insert(line - 1,text)
我将重命名子程序的参数:

text= LinkedList.List()

def insert_num(line_number, new_line):
    text.insert(line_number - 1,new_line)   

不要写“Ps对不起代码墙”,在问之前先把你的代码浓缩成a!请不要编辑问题以提及已回答。而是接受一个答案。