簡體   English   中英

使用隊列的Python程序

[英]Python program using queue

我想制作一個程序,其中使用隊列添加和刪除列表以及項目,但是我的代碼中存在需要修復的問題。 請幫幫我,謝謝。

class Queue:
    def __init__(self):
        self.items = ["Jene Dayao", "MJ Formaran", "Hans Matias", "Candy Santos", "Ian Domingo"]

    def view(self):
        print (self.items)

    def enqueue(self, item):
        item = input ("Name of the student: ")
        self.items.insert(item)

    def dequeue(self):
        return self.items.pop()
        print ("Student has finished photo taking!")

while True:
    
    print ("School ID Picture Taking Queue")
    print ("Select a function...")
    print ("")
    print ("1. See the student's active list")
    print ("2. A student has finished picture taking")
    print ("3. Add another student to the list")

    option = int(input("Enter your choice: "))

    if option == 1:
        view ()
    elif option == 2:
        enqueue ()
    elif option == 3:
        dequeue ()

python中的終極隊列實現

您的代碼存在一些問題:

  1. 您正在使用類概念並且您沒有初始化類的對象。 有兩種方法可以使代碼工作:

    • 初始化類的對象
    • 只使用函數並完全刪除使用類。
  2. enqueue 函數接受一個參數,但您沒有傳遞它。 相反,您正在從函數本身中獲取用戶的值。 你不能同時擁有它。 更好的解決方案是在函數之外獲取輸入。

  3. 選項的順序錯誤

  4. enqueue 函數使用帶有 2 個參數的 insert 函數。 相反,可以使用 append 函數。

我將解決所有問題

以下方法使用類。

class Queue:
    def __init__(self):
        self.items = ["Jene Dayao", "MJ Formaran",
                      "Hans Matias", "Candy Santos", "Ian Domingo"]

    def view(self):
        print(self.items)

    def enqueue(self, item):
        self.items.append(item)
        print("")

    def dequeue(self):
        print("Student has finished photo taking!")
        return self.items.pop(0)

queue = Queue() # initializing an object of the class Queue

while True:
    print("School ID Picture Taking Queue")
    print("Select a function...")
    print("")
    print("1. See the student's active list")
    print("2. Add another student to the list")
    print("3. A student has finished picture taking")

    option = int(input("Enter your choice: "))

    if option == 1:
        queue.view()
    elif option == 2:
        item = input("Name of the student: ") # taking the new students name
        queue.enqueue(item)
    elif option == 3:
        queue.dequeue()

下次 - 嘗試發布您遇到的錯誤,而不是寫“我的代碼中存在需要修復的問題”,以便人們知道如何幫助您。

這段代碼有多個問題:

  1. 您嘗試訪問屬於Queue類的函數,而無需對其進行初始化。 它應該是:
    # Must be initialized outside the loop, otherwise you will re-create 
    # it every time, discarding your changes
    queue = Queue()

    while True:

        ...

        option = int(input("Enter your choice: "))

        if option == 1:
            queue.view()
        elif option == 2:
            queue.enqueue()
        elif option == 3:
            queue.dequeue()
  1. 在函數enqueue您要求用戶輸入他想要添加的學生的姓名。 沒關系,但問題是您還期望函數從外部接收項目。 從函數簽名中刪除item ,保持如下所示:
def enqueue(self):
  1. 函數input用於從用戶接收數字。 要接收string ,這是學生姓名應該具有的類型,請使用raw_input
item = raw_input("Name of the student: ")
  1. self.items.insert(item)將不起作用,因為函數insert應該接收您要添加的項目及其在列表中的索引。 如果您不關心索引,只需使用:
self.items.append(item)
  1. 您錯誤地在選項 2 和 3 之間切換。當用戶選擇第三個選項時,您想調用q.enqueue ,將學生添加到列表中。 當用戶選擇第二個選項時,您想調用q.dequeue ,從列表中刪除學生:
q = Queue()
    if option == 1:
        q.view()
    elif option == 2:
        q.dequeue()
    elif option == 3:
        q.enqueue()

最后一件事 - 在使用它們之前閱讀您使用的函數的文檔是一個好習慣。 有時,您可能會覺得看到他們的名字就知道如何稱呼他們,但正如您所見,情況並非總是如此。

此致。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM