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 != "": self.insert_update(t,"") 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-01-23 20:01:34 浏览: 109
看到你修改的代码中,将插入操作的第二个参数设为 "",这样会导致插入的单词没有定义,而且会覆盖已有单词的定义。因此,你需要将插入操作的第二个参数改为单词的定义。
可以将 insert_update 方法的第二个参数改为一个空字符串,改为一个空字典,或者省略不写。这样当用户输入 "insert word definition" 时,就可以通过 st[2:] 得到单词的定义,然后将它们组成一个字符串作为 insert_update 方法的第二个参数。
修改后的代码如下:
```
elif s == "insert":
if t != "" and len(st) > 2:
definition = " ".join(st[2:])
self.insert_update(t, definition)
```
这样,当用户输入 "insert word definition" 时,程序就会将 "word" 存储在 t 变量中,将 "definition" 存储在 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)
```
检查代码并修改:class Node: def init(self, data): self.data = data self.prev = None self.next = None Class DoublyLinkedlist: # def init(self) self.head = None def remove(self,item): current = self.head previous = None found = False while not found: if current.getData() == item found = True else: previous = current current = current.getNext() if previous == None: self.head = current.getNext() current.prev = None else: previous.setNext(current.getNext()) current.prev = previous
class Node:
def __init__(self, data):
self.data = data
self.prev = None
self.next = None
class DoublyLinkedList: # 类名应该是DoublyLinkedList而不是DoublyLinkedlist
def __init__(self):
self.head = None
def append(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
else:
current = self.head
while current.next is not None:
current = current.next
current.next = new_node
new_node.prev = current
def prepend(self, data):
new_node = Node(data)
if self.head is None:
self.head = new_node
else:
self.head.prev = new_node
new_node.next = self.head
self.head = new_node
def delete(self, data):
current = self.head
while current is not None:
if current.data == data:
if current.prev is not None:
current.prev.next = current.next
else:
self.head = current.next
if current.next is not None:
current.next.prev = current.prev
return
current = current.next
def print_list(self):
current = self.head
while current is not None:
print(current.data)
current = current.next
阅读全文