-2

I am a newbie to python OOP concept. I need to write a program to manipulate a list of items(add, delete and display) using class. Without class i got output in this way :

def add():
    b=input("How many inputs? : ")
    print b
    if b==1:
        in1=input("Enter item : ")
        a.append(in1)
    else:
        in2=input("Enter items : ")
        a.extend(in2)
    print a

def delete():
    c=input("which item to delete? ")
    global a
    a=[x for x in a if x != c]
    print a

def display():      
    print a

ans=True
global a
a=[1,2,3,5,2]
while ans:
    print ("""
    1.Add items
    2.Delete items
    3.Display items
    4.Exit/Quit
    """)
    ans=raw_input("What would you like to do? ") 
    if ans=="1": 
        add()
    elif ans=="2":
        delete()
    elif ans=="3":
        display() 
    elif ans=="4":
        print("\n Goodbye") 
        exit()
    else:
      print("\n Not Valid Choice Try again") 

Can anyone help me to write the same code using class?

NooNa MarJa
  • 535
  • 6
  • 14

1 Answers1

1

Just put all your functions in a class....

The advantage to the class approach is that you can have multiple lists being manipulated at once, and when you use a class, it doesn't depend on an arbitrary global list name you have picked.

Additionally, you were using the input method. As it seems you're using Python 2.7 (judging from your print statements), you should be using raw_input. input tries to eval what it gets back, whereas you just want the raw string that the user has entered, which is what raw_input returns.

 class ItemList:
        def __init__(self, list):
            self.a = list

        def add(self):
            b=raw_input("How many inputs? : ")
            print b
            if b==1:
                in1=raw_input("Enter item : ")
                self.a.append(in1)
            else:
                in2=raw_input("Enter items : ")
                self.a.extend(in2)
            print self.a

        def delete(self):
            c=raw_input("which item to delete? ")
            self.a=[x for x in a if x != c]
            print self.a

        def display(self):      
            print self.a


ans = True
a=[1,2,3,5,2]
myList = ItemList(a)
while ans:
    print ("""
    1.Add items
    2.Delete items
    3.Display items
    4.Exit/Quit
    """)
    ans=raw_input("What would you like to do? ") 
    if ans=="1": 
        myList.add()
    elif ans=="2":
        myList.delete()
    elif ans=="3":
        myList.display() 
    elif ans=="4":
        print("\n Goodbye") 
        exit()
    else:
      print("\n Not Valid Choice Try again") 
Community
  • 1
  • 1
River
  • 8,585
  • 14
  • 54
  • 67
  • Thanq..it helped..yeah..i am using 2.7 version..tanq for the gud info..but input takes integer and raw_input for string input na ? I am sorry if itz useless question..itz only months i am into programming.. – NooNa MarJa Feb 05 '16 at 16:16
  • @NooNaMarJa I'm not sure what you're asking. The link I provided can probably help some. `input` does only work for numbers (in your case at least), whereas `raw_input` works for other things too. I figured you might want to add strings or other data to your list besides just numbers. – River Feb 05 '16 at 17:29