Issue
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()
Solution
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)
Answered By - Martin Valgur
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.