首页 > 解决方案 > 不断报错,无法实现输出

问题描述

我正在尝试实现此输出,但出现错误。我运行我的代码但出现错误。我正在尝试这样做:

呼叫下一位客户。

  1. 如果等待队列为空,则显示等待队列中没有客户
  2. 否则,显示等待队列中客户的队列号
  3. 如果顾客响应了呼叫,则将他从等待队列中移出并放入另一个“顾客服务”队列,以表明顾客已经拿到了食物
  4. 显示一条消息以指示正在为客户提供服务

预期输出:

请选择:1

可以告诉我你的名字:Fad

我在运行时遇到的错误:

TypeError
Traceback (most recent call last)
<ipython-input-16-85165eb1f15c> in <module>
     44     choice = int(input("Please choose:"))
     45     if choice == 1:
---> 46         register(wait_que, que_num)
     47         que_num += 1 # queue number will be incremented by 1. so if 100, then after that is 101
     48     elif choice == 2:
<ipython-input-16-85165eb1f15c> in register(queue, qnum)
     15 def register(queue, qnum):
     16     name = input("May I have your name ")
---> 17     queue.append[qnum, name, 0]
     18
     19     pass
TypeError: 'builtin_function_or_method' object is not subscriptable**

我的代码:

import random
from collections import deque
from time import sleep

print("Menu")
print("1. Register a customer")
print("2. Call next customer")
print("3. List customers in queue")
print("4. Exit")

def menu():
    pass

def register(queue, que_num):
    name = input("May I have your name ")
    queue.append[que_num, name, 0]
    pass

def call_next():
    if queue.empty:
        print("No customer in waiting queue")
    else:
        return 0
    pass

def list_cust():
    pass

wait_que = deque([]) # assign nothing to it
que_num = random.randint(1,5)*100
choice = ''

while choice != 0:
    menu() # print menu
    choice = int(input("Please choose:"))
    if choice == 1:
        register(wait_que, que_num)
        que_num += 1 # queue number will be incremented by 1. so if 100, then after that is 101
    elif choice == 2:
        call_next()
    elif choice == 3:
        list_cust()
    elif choice == 0:
        print("End of program")

    else:
        print("Invalid option...")

标签: pythonstackqueue

解决方案


append是python中列表的一种方法。因此,您必须使用括号来利用该功能。

改变

def register(queue, que_num):
             name = input("May i have your name ")
             queue.append[que_num, name, 0]

至:

def register(queue, que_num):
    name = input('May i have your name ')
    queue.append([que_num, name, 0])

推荐阅读