-3

How to reverse order the list containing strings where def view(self), because with reversed() I get an error that it can't be done with strings. Any help?

class Stack():

    def __init__(self):
        self.stack = []

    def view(self):
        for x in reversed(self.stack):
            print(self.stack[x])

    def push(self):
        item = input("Please enter the item you wish to add to the stack: ")
        self.stack.append(item)

    def pop(self):
        item = self.stack.pop(-1)
        print("You just removed item: {0}".format(item))

stack = Stack()
Martynas
  • 13
  • 1
  • 6

1 Answers1

0

for x in list(...): sets x to each element of the list. You are might be confusing it with iterating over a dictionary, where x would be set to the key of each element.

def view(self):
    for x in reversed(self.stack):
        print(x)
Martin Valgur
  • 5,087
  • 32
  • 42