问题
class Queue():
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def Enqueue(self,item):
return self.items.insert(0, item)
def Size(self):
return len(self.items)
def Dequeue(self):
return self.items.pop()
Q = Queue()
def Hotpot(namelist,num):
for name in namelist:
Q.Enqueue(name)
while Q.Size() > 1:
for i in range(num):
Q.Enqueue(Q.Dequeue())
Q.Dequeue()
return Q.Dequeue() # I would like to print and see what is getting removed, I tried with x = Q.Dequeue(), print x and print Q.Dequeue() but im getting "None"
print Hotpot(['A','B','C','D','E','F'],7)
Hi Team, I am trying above code for checking Queue, here I would like to print which value is removing each cycle. While printing I am getting none oly, please help me what changes I have to do for my expectation.
回答1:
If you want to know what's goingto be returned, you need to save it locally, print what you saved, then return what you saved. Something like:
x = Q.Dequeue()
print(x)
return x
回答2:
Your code works for me, when you change Q.Dequeue() by print Q.Dequeue().
Better python would be:
from collections import deque
def hotspot(names, num):
queue = deque(reversed(names))
while len(queue)>1:
queue.rotate(num)
print queue.pop()
return queue.pop()
print hotspot(['A','B','C','D','E','F'], 7)
来源:https://stackoverflow.com/questions/25153300/how-to-print-return-value-of-method-in-python