AttributeError: 'list' object has no attribute 'apend'

1

Why does this error occur?

  

"AttributeError: 'list' object has no attribute 'append'" with this code?

class Stack :
  def __init__(self) :
    self.items = []

  def push(self, item) :
    self.items.apend(item)

  def pop(self) :
    return self.items.pop()

  def isEmpty(self) :
    return (self.items == [])

  s = Stack()
  s.push(54)
  print s.pop()
    
asked by anonymous 24.02.2017 / 16:48

1 answer

0

It was just a typo. You forgot a 'p' in 'append'

Switch

  

self.items.apend(item)

by

  

self.items.append(item)

In addition, you need to retract this block to the correct scope.

    s = Stack()   
    s.push(54)  
    print s.pop()

The final code would look like this

class Stack :
  def __init__(self) :
    self.items = []

  def push(self, item) :
    self.items.append(item)

  def pop(self) :
    return self.items.pop()

  def isEmpty(self) :
    return (self.items == [])


s = Stack()
s.push(54)
print(s.pop())

See working online here

    
24.02.2017 / 17:41