Mar-08-2017, 03:39 AM
I want to create two objects of a class. Using these two objects i want to input some numbers into a list which is a member of the class. After inputting data it will display the content of the list. Simple code. But the output was not which i want. I enclosed my python code and the output. Please tell me why the program is not working. I am new programmer in Python.
class Demo: arr = [] n = 0 def __init__(self,s): self.n=s def fill(self): print("Enter elements in array ") for i in range(self.n): x=input() self.arr.append(x) def show(self): for i in range(self.n): print(self.arr[i]) obj1 = Demo(5) obj2 = Demo(3) obj1.fill() obj2.fill() print("Data from first Object") obj1.show() print("Data from second object") obj2.show()
Output:Enter elements in array
1
2
3
4
5
Enter elements in array
6
7
8
Data from first Object
1
2
3
4
5
Data from second object
1
2
3