Im using a different value, got an error, help?

https://app.dataquest.io/m/1000352/object-oriented-python-practice-problems/6/supermarket-queue

class SupermarketQueue():
    
    def __init__(self):
        self.elements=[]
        
    def add_at_back(self,element):
        self.elements.append(element)
        
    def remove_from_front(self):
        self.elements.remove(self.elements[0])
        return self.elements[0]
    
    def __len__(self):
        return len(self.elements)
    
    def __str__(self):
        self.elements.reverse()
        return str(self.elements)

hi please help, i get error message: Method __str__ returned the wrong value

It will reverse list every time you print it. check by printing multiple times.

print(queue)
print(queue)
print(queue)

['Bob', 'Alice']
['Alice', 'Bob']
['Bob', 'Alice']

So instead of reverse actual data; First store in temp and then reverse temp.

    def __str__(self):
        temp = self.elements.copy()
        temp.reverse()
        return str(temp)

Now you can see it’s stationary

print(queue)
print(queue)
print(queue)

['Bob', 'Alice']
['Bob', 'Alice']
['Bob', 'Alice']


There is one more issue in

Here you have to return element that you have removed from list. Not the first element of list after removed front element. so it should be

    def remove_from_front(self):
        rm_element = self.elements[0]
        self.elements.remove(rm_element)
        return rm_element
2 Likes