好吧,所以即時嘗試在堆棧中輸入一個單詞,我想輸入一個字符串後打印所有這些。所以我一次只能打印一份。我嘗試在外面使用for循環,但Stacks顯然不可迭代。所以我在堆棧中迭代它。它仍然沒有工作。棧迭代python3
class Stack:
def __init__(self):
self.items = []
def push(self,items):
self.items.insert(0,items)
def pop(self):
for x in self.items:
print(self.items.pop(0))
def show(self):
print (self.items)
s = Stack()
s.show()
placed = input("enter")
item = s.pop()
print(item, "is on top", s)
關於你的棧的設計,爲什麼不使用'.append()'和'。流行()'而不是?將項目添加到列表的末尾,並且不帶參數的'.pop()'從末尾再次移除它們。 –