这段代码里的插入有问题def process(self): self.read() while True: st = input(">").strip().lower() st = st.split(" ") s = st[0] t ="" if len(st) == 2: t = st[1] if s == "show": self.show(t) elif s == "seek": if t != "": self.seek(t) elif s == "translate": if t != "": self.translate(t) elif s == "insert": if t != "" and len(st) > 2: definition = " ".join(st[2:]) self.insert_update(t, definition) elif s == "delete": if t != "": self.delete(t) elif s == "file": fileName = t if fileName != "" and os.path.exists(fileName): self.scanFile(fileName) elif s == "web": url = t if url != "" and url.startswith("http://"): self.scanWeb(url) elif s == "exit": break else: print("显示单词 show words") print("查找单词 seek word") print("翻译单词 translate word") print("增加单词 insert word") print("删除单词 delete word") print("文件获取 file fileName") print("网络获取 web url") print("退出程序 exit") self.save()
时间: 2024-02-10 08:04:34 浏览: 30
这段代码中的插入操作需要输入两个参数,第一个参数是要插入的单词,第二个参数是单词的定义。如果输入的参数个数小于2,则无法执行插入操作,因为没有定义可供插入。
另外,这段代码中的 `self.insert_update(t, definition)` 需要进一步确认 `insert_update` 方法是否已经定义在该类中。如果没有定义,则无法进行插入操作。
如果您能提供更具体的错误信息,我可以更好地帮助您解决问题。
相关问题
约瑟夫环改错class Node: def __init__(self,data): self.data=data self.next=Noneclass linklist: def __init__(self): self.head=None self.data=None def isEmpty(self): if self.head: return False else: return True def length(self): if self.isEmpty(): return 0 else: t = self.head n = 1 while t.next: if t.next == self.head: break t = t.next n = n + 1 return n def addhead(self,data): node = Node(data) if self.isEmpty(): self.head = node self.tail = self.head else: node.next = self.head self.head = node self.tail.next = self.head def addtail(self,data): node=Node(data) if self.isEmpty(): self.addhead(data) else: t=self.head n=1 l=self.length() while n<l: n=n+1 t=t.next t.next=node node.next=self.head self.tail=node def delete(self,index): if self.isEmpty(): print("The linked list is empty") else: t = self.head l = self.length() if index == 0: self.head = t.next self.tail.next = self.head elif index == l - 1: n = 1 while n < l - 1: t = t.next n = n + 1 t.next = self.head self.tail = t elif index > l - 1: print("Out of range") elif index < 0: print("Wrong operation") else: n = 1 while n < index - 1: t = t.next n = n + 1 a = t.next.next t.next = a def insert(self,data,index): l = self.length() if index == 0 or self.isEmpty(): self.addhead(data) elif index >= l: self.addtail(data) else: node = Node(data) t = self.head n = 1 while n < index - 1: t = t.next n = n + 1 a = t.next t.next = node node.next = a def search(self,a): t=self.head for i in range(a): t=t.next return t.data def form(self,datalist): self.addhead(datalist[0]) for i in range(1,len(datalist)): self.addtail(datalist[i]) t = self.head while t.next != self.head: t = t.nextn,p=map(int,input().split(' '))data=[]p=p-1for i in range(1,n+1): data.append(i)print(data)datalist=[]for i in range(len(data)): datalist.append(int(data[i]))link=linklist()link.form(datalist)a=pb=[]while link.length()>0: b.append(link.search(a)) link.delete(a) a=a+p while a>=link.length(): a=a-link.length()print(b)
改错如下:
```python
class Node:
def __init__(self, data):
self.data = data
self.next = None
class LinkList:
def __init__(self):
self.head = None
self.tail = None
def isEmpty(self):
if self.head:
return False
else:
return True
def length(self):
if self.isEmpty():
return 0
else:
t = self.head
n = 1
while t.next:
if t.next == self.head:
break
t = t.next
n += 1
return n
def addhead(self, data):
node = Node(data)
if self.isEmpty():
self.head = node
self.tail = self.head
else:
node.next = self.head
self.head = node
self.tail.next = self.head
def addtail(self, data):
node = Node(data)
if self.isEmpty():
self.addhead(data)
else:
t = self.head
n = 1
l = self.length()
while n < l:
n += 1
t = t.next
t.next = node
node.next = self.head
self.tail = node
def delete(self, index):
if self.isEmpty():
print("The linked list is empty")
else:
t = self.head
l = self.length()
if index == 0:
self.head = t.next
self.tail.next = self.head
elif index == l - 1:
n = 1
while n < l - 1:
t = t.next
n += 1
t.next = self.head
self.tail = t
elif index > l - 1:
print("Out of range")
elif index < 0:
print("Wrong operation")
else:
n = 1
while n < index:
t = t.next
n += 1
a = t.next.next
t.next = a
def insert(self, data, index):
l = self.length()
if index == 0 or self.isEmpty():
self.addhead(data)
elif index >= l:
self.addtail(data)
else:
node = Node(data)
t = self.head
n = 1
while n < index:
t = t.next
n += 1
a = t.next
t.next = node
node.next = a
def search(self, a):
t = self.head
for i in range(a):
t = t.next
return t.data
def form(self, datalist):
self.addhead(datalist[0])
for i in range(1, len(datalist)):
self.addtail(datalist[i])
t = self.head
while t.next != self.head:
t = t.next
n, p = map(int, input().split(' '))
data = [i for i in range(1, n+1)]
print(data)
datalist = []
for i in range(len(data)):
datalist.append(data[i])
link = LinkList()
link.form(datalist)
a = p-1
b = []
while link.length() > 0:
b.append(link.search(a))
link.delete(a)
a += p-1
while a >= link.length():
a -= link.length()
print(b)
```
@classmethod def setUpClass(self): self.process = process self.cns = ConnectSrt(process=self.process) last_result = None def tearDown(self): self.last_result = self._outcome.result
这段代码存在一些语法错误,需要进行一些修改。具体来说,`tearDown()` 方法的缩进不正确,应该与 `setUpClass()` 方法保持一致。同时,`last_result` 变量没有定义,应该修改为 `self.last_result`。
修改后的代码如下所示:
```python
import unittest
class MyTest(unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.process = process
cls.cns = ConnectSrt(process=cls.process)
cls.last_result = None
def tearDown(self):
self.last_result = self._outcome.result
```
在修改后的代码中,我们将 `setUpClass()` 方法和 `tearDown()` 方法的缩进进行了调整,并且将 `last_result` 变量修改为了 `self.last_result`。这样就可以在 `tearDown()` 方法中保存上一条测试用例的执行结果了。
相关推荐
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)