【发布时间】:2026-01-09 04:35:01
【问题描述】:
我正在尝试将 3 个人排入列表中,以在顶部显示每个人的所有姓名的结果,但只能获得一个没有任何姓名的结果:
Contacting the following
Phone answered: Yes
Booked an appointment: No
Reshedule an appointment again.
我想让输出显示他们的所有姓名和 3 个输出,每个人一个来自存储在 'names' 中的信息,并且每个名称不会出现两次。
我想使用队列根据列表对它们进行优先级排序,因此我试图将它们按顺序排列。 if 和 elif 是根据随机生成器属于任一类别的条件。现在,只是没有定义包含名称的方法。
代码
import random
class Queue:
def __init__(self):
self.container = []
def isEmpty(self):
return self.size() == 0
def enqueue(self, item):
self.container.append(item)
def dequeue(self):
self.container.pop(0)
def size(self):
return len(self.container)
def peek(self) :
return self.container[0]
names = ["Alvin", "James", "Peter"]
# Enqueuing
q = Queue()
q.enqueue(random.choice(names))
# Dequeuing and Printing
print("Contacting the following:\n" + "\n".join(q.container)) # unsure about this
for i in range(q.size()):
answered = random.randint(0,1)
booked = random.randint(0, 1)
if(answered == 1 and booked == 1):
print("Now Calling -" + (q.names)) # unsure about this
print("Phone answered: Yes")
print("Booked an appointment: Yes")
print("Booking successful.")
elif(answered==1 and booked==0):
print("Now Calling -" + (q.names)) # unsure about this
print("Phone answered: Yes")
print("Booked an appointment: No")
print("Reshedule an appointment again.")
elif(answered == 0):
print("Now Calling -" + (q.names)) # unsure about this
print("Phone answered: No")
print("Reshedule a callback.")
q.dequeue()
所需输出示例:
Contacting the following
Alvin
James
Peter
Now Calling - James
Phone answered: No
Reshedule a callback.
【问题讨论】:
-
当你删除它时,我正在给你的previous question写一个答案......
-
很抱歉。我以为我会自己想办法。
标签: python data-structures queue