python - 不断报错,无法实现输出
问题描述
我正在尝试实现此输出,但出现错误。我运行我的代码但出现错误。我正在尝试这样做:
呼叫下一位客户。
- 如果等待队列为空,则显示等待队列中没有客户
- 否则,显示等待队列中客户的队列号
- 如果顾客响应了呼叫,则将他从等待队列中移出并放入另一个“顾客服务”队列,以表明顾客已经拿到了食物
- 显示一条消息以指示正在为客户提供服务
预期输出:
请选择: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...")
解决方案
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])
推荐阅读
- r - 使用查找表替换部分字符串
- python - 当元素的顺序发生变化时通过xpath提取元素(即使相对xpath也在变化)
- neo4j - Neo4j - 从嵌套的 JSON 文件和列表创建节点
- angular - 如果使用 [].concat() 组合多个数组,Angular @NgModule 导入不会看到模块
- numpy - numpy 点积的爱因斯坦符号
- c# - 使用 MS Graph 进行机器人身份验证 - 如何在不手动输入的情况下动态读取 6 位代码
- c# - c#aspx如何在页面上执行拆分
- python - Python3 线程还是 aiohttp?
- c# - 将部分视图转换为 HTML 2019
- ruby - 使用 ruby Octokit,我如何返回自指定日期以来创建的所有拉取请求