FIFO类实现不起作用

时间:2015-01-31 20:42:40

标签: python python-3.x

我在下面创建了一个简单的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!

然而,当在另一个文件中使用该类时,dequeue()方法似乎不起作用,我正在尝试实现一个杂货店阵容,其中的需求如下:

该计划绝不允许超过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")

我知道它实际上会将名称添加到列表中但我不知道为什么当输入为“Serve”时它什么都不做

同样在我想要打印的时候(“排队空”),它甚至没有达到那条线。想法?

3 个答案:

答案 0 :(得分:2)

在我看来,您的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")

答案 1 :(得分:1)

您应该只使用带有条件分支的一个来管理不同的命令(添加/服务器/退出等),而不是使用多个while(带有一些imbricated)。修改起来会更清晰,更容易。

我建议这样的事情:

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: ")

答案 2 :(得分:-1)

我解决了!这是我使用该类的最终代码:

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: ")