Attributeerror: 'list' Object has no attribute 'apend'

Asked

Viewed 968 times

1

Why does this errp occur?

"Attributeerror: 'list' Object has no attribute 'apend'" 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()
  • Which language is using? is most likely to be self.items.append(item) rather than self.items.apend(item)

1 answer

0


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

Change

self.items.apend(item)

for

self.items.append(item)

Also, you need to move this block back 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

  • Gee, thanks, I was wrong on the site where I’m learning...

  • If your problem has been solved, consider accept the answer by clicking the green v on the left side of the question. .

Browser other questions tagged

You are not signed in. Login or sign up in order to post.