FIFO Class Implemenation Not Working
我在下面创建了一个简单的FIFO类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | class Queue: # Constructor, which creates a new empty queue: def __init__(self): self.items = [] # TODO: You must implement this constructor! # Adds a new item to the back of the queue, and returns nothing: def queue(self, item): return self.items.append(item) # TODO: You must implement this method! # Removes and returns the front-most item in the queue. # Returns nothing if the queue is empty. def dequeue(self): if len(self.items)==0: return None else: return self.items.pop(0) # TODO: You must implement this method! # Returns the front-most item in the queue, and DOES NOT change the queue. def peek(self): if len(self.items)==0: return None else: return self.items[0] # TODO: You must implement this method! # Returns True if the queue is empty, and False otherwise: def is_empty(self): return self.items == [] # TODO: You must implement this method! # Returns the number of items in the queue: def size(self): return len(self.items) # TODO: You must implement this method! # Removes all items from thq queue, and sets the size to 0: def clear(self): while len(self.items) > 0: self.items.pop() # TODO: You must implement this method! # Returns a string representation of the queue: def __str__(self): return repr(self.items) # TODO: You must implement this method! |
但是,当在另一个文件中使用该类时,dequeue()方法似乎不起作用,我正在尝试实现一个杂货店队列,其中的需求如下:
该计划不应允许超过3人排队曾经。如果用户试图在以下情况下向配置列表中添加另一个人:已经有3个人了,程序应该打印一个错误留言并继续。
如果用户试图在没有人的情况下为下一个人服务,排队,程序应该通知用户排队是空的。当一个人被服务时,程序应该打印这个人的名字人。
这是我尝试实现和使用类时的另一个文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | from queue import Queue q=Queue() exit==False ase=input("Add, Serve, or Exit:") while ase=="Serve" and q.is_empty()== True and exit==False: print("The lineup is already empty.") ase=input("Add, Serve, or Exit:") if ase=="Exit": exit==True while (q.size()<3 and ase=="Add") or (q.size()<3 and ase=="Serve"): add=input("Enter the name of the person to add:") ase=input("Add, Serve, or Exit:") q.queue(add) print(q.__str__()) while q.size()==3 and ase=="Add": print("You cannot add more people, the lineup is full!") ase=input("Add, Serve, or Exit:") while ase=="Serve" and q.is_empty()==False and exit==False: print(q.dequeue(),"has been served") |
我知道它实际上在列表中添加了名称,但我不知道当输入为"服务"时为什么它什么也不做。
同样在乞讨的时候,当我想要它打印("列队空"),它甚至没有到达那一行。思想?
在我看来,您的
1 2 3 4 5 6 7 8 9 10 11 12 | while ase=="Serve" and q.is_empty()== True and exit==False: print("The lineup is already empty.") ase=input("Add, Serve, or Exit:") if ase=="Exit": exit==True # ^^^^^^^^^^ ... while ase=="Serve" and q.is_empty()==False and exit==False: # ^^^^^^^^^^^ print(q.dequeue(),"has been served") |
您不应该使用多个while(有些地方使用了覆写的while),而应该只使用一个具有条件分支的while来管理不同的命令(add/server/exit等)。它将更加清晰和易于修改。
我建议这样做:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | ase = raw_input("Add, Serve, or Exit:") while ase !="Exit": if ase =="Serve": if q.is_empty(): print("The lineup is already empty.") else: # dequeue + message elif ase =="Add": if q.size() == 3: print("You cannot add more people, the lineup is full!") else: add = raw_input("Enter the name of the person to add:") # enqueue add + message else: print("invalid command...") ase = raw_input("Add, Serve, or Exit:") |
我解决了!下面是关于使用类的最终代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | from queue import Queue q=Queue() exit==False ase=input("Add, Serve, or Exit:") while ase=="Serve" and q.is_empty()==True: print("The lineup is already empty.") ase=input("Add, Serve, or Exit:") if ase=="Exit": break while (q.size()<3 and ase=="Add"): add=input("Enter the name of the person to add:") ase=input("Add, Serve, or Exit:") q.queue(add) print(q.__str__()) while q.size()==3 and ase=="Add": print("You cannot add more people, the lineup is full!") ase=input("Add, Serve, or Exit:") while ase=="Serve": print(q.dequeue(),"has been served") ase=input("Add, Serve, or Exit:") |