0

I have the following code:

class Cola:
    def __init__(self):
        self.cola = [] 
    def add_list(self, element):
        self.cola.append(element)
    def numElements(self):
        return len(self.cola) 

def list_entry():
    membership= input("  Membership card? (y/n):  ")
    name= input("  Name:  ")
    lastname= (input("  Lastname:  "))
    return (membership, name, lastname)

def add_list():
    new= list_entry()
    Cola().add_list(new)

n = int(input("Choose an option\n   1.Add to waiting list\n   2.Show waiting customers\n   3.Close for today\n Option:   ")) 
while n<5:
    if n==1:
        add_list()
        n = int(input("Choose an option\n   1.Add to waiting list\n   2.Show waiting customers\n   4.Close for today\n Option:   ")) 
    elif n==2:
        waiting= Cola().numElements()
        print(waiting)
        n = int(input("Choose an option\n   1.Add to waiting list\n   2.Show waiting customers\n   4.Close for today\n Option:   ")) 
    elif n==3:
        print("See you")
        n=5

The problem is that I expect that when user choose 3, the number of instance in the list should appear but instead I always get 0, which tells me that I´m not really adding anything to the list What am I doing wrong?

Extra: I suspect that for on the long run a list of lists will be more useful How can I do that?

martineau
  • 119,623
  • 25
  • 170
  • 301
René Martínez
  • 179
  • 1
  • 3
  • 11
  • 1
    You need to look into how class instances work. I don't see you initializing an instance of a class anywhere in this code and every time you use the class Cola you are using anonymous functions of this class – Rashid 'Lee' Ibrahim Mar 16 '20 at 01:23
  • Stack Overflow is not a substitute for guides, tutorials or documentation, which are what you need. – AMC Mar 16 '20 at 03:15

1 Answers1

4

Whenever you say Cola() you are creating a new object that is of class Cola. You need to store the item in a variable. Also, you can turn the functions you have created into methods too:

class Cola:
    def __init__(self):
        self.cola = []
    def add_list(self, element):
        self.cola.append(element)
    def numElements(self):
        return len(self.cola)

def list_entry():
    membership= input("  Membership card? (y/n):  ")
    name= input("  Name:  ")
    lastname= (input("  Lastname:  "))
    return (membership, name, lastname)


cola_item = Cola()

n = int(input("Choose an option\n   1.Add to waiting list\n   2.Show waiting customers\n   3.Close for today\n Option:   "))
while n<5:
    if n==1:
        cola_item.add_list(list_entry())
        n = int(input("Choose an option\n   1.Add to waiting list\n   2.Show waiting customers\n   3.Close for today\n Option:   "))
    elif n==2:
        waiting= cola_item.numElements()
        print(waiting)
        n = int(input("Choose an option\n   1.Add to waiting list\n   2.Show waiting customers\n   3.Close for today\n Option:   "))
    elif n==3:
        print("See you")
        n=5

(I assumed your number system was a 1, 2, or 3) Hope this helps.

Also to address your extra question, if you do it the way i suggest then you are essentially doing exactly this, except you are creating a list of tuples. If you put:

for item in cola_item.cola:
    print(item)

inside your elif n==2 block, you can see what it prints out. If you explicitly want a list of lists, then you can change return (membership, name, lastname) to return [membership, name, lastname]

Here is the difference between a list and a tuple

Parakiwi
  • 591
  • 3
  • 19