Share your repls and programming experiences

← Back to all posts
cloud
xanthsalas (2)

i have some problem about queueing in python ...

heres my source code and i dont know how to fix this ...

class queue:
def init(self):
self.item=[]

def empty(self):
return self.item==[]

def enqueue(self):
self.item.append=[]

def dequeue(self):
return self.item.pop==[]

n=queue()
print(n.empty)
n.enqueue(input(""))

print("push:",n.item)
n.enqueue(input(""))

print("push:",n.item)
n.enqueue(input(""))

print("push:",n.item)
n.enqueue(input(""))

print("push:",n.item)
n.enqueue(input(""))

n.dequeue()
print("dequeue",n.dequeue())

it says class object ...

Commentshotnewtop
mat1 (3443)

what is the problem? Can you please put this in a repl to make it easier to debug?

pyelias (1787)

There are quite a few problems here, so I'll just comment the code with the issues:

class queue:
  def __init__(self):
    self.item=[]

  def empty(self):
    return self.item==[]

  def enqueue(self):
    self.item.append=[] # you shouldn't do .append=whatever, you want .append(whatever)
    # also, the only thing you enqueue here is [], so you might want to take an argument and pass that instead

  def dequeue(self):
    return self.item.pop==[] # you don't want .pop==[], you just want .pop()

n=queue()
print(n.empty) # you probably want .empty() here or you'll print the actual function, not the result
n.enqueue(input(""))

print("push:",n.item)
n.enqueue(input(""))

print("push:",n.item)
n.enqueue(input(""))

print("push:",n.item)
n.enqueue(input(""))
  
print("push:",n.item)
n.enqueue(input(""))

n.dequeue() # you dequeue twice here, not sure if this is intentional
print("dequeue",n.dequeue()) 

The revised code:

class queue:
  def __init__(self):
    self.item=[]

  def empty(self):
    return self.item==[]

  def enqueue(self, thing_to_enqueue):
    self.item.append(thing_to_enqueue)

  def dequeue(self):
    return self.item.pop()

n=queue()
print(n.empty())
n.enqueue(input(""))

print("push:",n.item)
n.enqueue(input(""))

print("push:",n.item)
n.enqueue(input(""))

print("push:",n.item)
n.enqueue(input(""))
  
print("push:",n.item)
n.enqueue(input(""))

print("dequeue",n.dequeue())
_Nobody_ (0)

Hey, n.empty is not a function call. print(n.empty()) will fix it.

xanthsalas (2)

@Dogdin sir my program saya that bound method queue. empty of <main. queue object at 0x013E6FD0>>

what does it mean sir..

xanthsalas (2)

@Dogdin can u debug my program sir...

Dogdin (14)

@xanthsalas Sorry, I can't. But thanks for calling me 'sir'.