FIFO类实行不工作
我创建了下面一个简单的FIFO类:FIFO类实行不工作
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!
然而,在另一个文件中使用类时,出列()方法似乎不工作,我想实现一个杂货店阵容的需求如下:
该计划不应该允许超过3人在 阵容一次。如果用户试图在 已有3人时尝试将其他人添加到该阵容中,则该程序应打印错误 消息并继续。
如果用户在 阵容中没有人时尝试服务下一个人,程序应通知用户该阵容为空。 当一个人被送达时,该程序应该打印该人的名字 。
这是我的其他文件,当我尝试实现和使用类:
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")
我知道,这实际上增加了名称列表中,但我不知道为什么它什么也不做,当输入“服务”
也在乞讨时,我想它打印(“排队空”),它甚至不会到那条线。思考?
无需使用多种而(有一些鳞片状),你应该使用只有一个条件分支来管理不同的命令(添加/服务器/退出等)。这将更加清晰和容易修改。
我建议是这样的:
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: ")
在我看来,你的exit
变量有问题:它在第一个循环结束时设置为True
。但是,你的Serve
循环希望它是False
进入...
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")
我现在修好了!但你能帮我解决这个问题吗?如果队列是空的但它看起来不起作用,我希望它打印“Lineup full”:ase = input(“Add,Serve或Exit:”)while ase ==“Serve”and q.is_empty() ==假: 打印(“该阵容已经是空的。”) ase =输入(“添加,服务或退出:”) 如果ase ==“退出”: break – 2015-01-31 21:00:47
我解决它!下面是我在使用类最终代码:
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: ")
这并不能解释什么问题是。 – 2015-01-31 22:09:07
非常好。我已经自己解决了,但我认为这是比我更好的答案。 – 2015-01-31 21:25:00