2015-01-31 161 views
2

我創建了下面一個簡單的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") 

我知道,這實際上增加了名稱列表中,但我不知道爲什麼它什麼也不做,當輸入「服務」

也在乞討時,我想它打印(「排隊空」),它甚至不會到那條線。思考?

回答

1

無需使用多種而(有一些鱗片狀),你應該使用只有一個條件分支來管理不同的命令(添加/服務器/退出等)。這將更加清晰和容易修改。

我建議是這樣的:

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

非常好。我已經自己解決了,但我認爲這是比我更好的答案。 – 2015-01-31 21:25:00

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") 
+0

我現在修好了!但你能幫我解決這個問題嗎?如果隊列是空的但它看起來不起作用,我希望它打印「Lineup full」:ase = input(「Add,Serve或Exit:」)while ase ==「Serve」and q.is_empty() ==假: 打印(「該陣容已經是空的。」) ase =輸入(「添加,服務或退出:」) 如果ase ==「退出」: break – 2015-01-31 21:00:47

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

這並不能解釋什麼問題是。 – 2015-01-31 22:09:07