【问题标题】:Keep getting errors and cannot achieve output不断报错,无法实现输出
【发布时间】:2020-05-21 14:45:26
【问题描述】:

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

呼叫下一位客户。

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

【问题讨论】:

    标签: python stack queue


    【解决方案1】:

    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])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-13
      • 1970-01-01
      • 2019-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多