Home How to run a class function

# How to run a class function

John Rawls
1#
John Rawls Published in 2018-01-12 21:01:57Z
 I was trying to write some code that would take in the number of sides of a polygon and a user can just randomly input the length of each side I wanted to make a separate class polygon and the following was my code: #numSides=int(input(Number of sides)) class polygon(object): def __init__(self,x): #useless code self.x=x #useless code self.x=[] #useless code def __inputSides__(self): #counter=0 #for i in range(x): # counter=counter+1 numSides=int(input("number of sides")) list_=[] for i in range(numSides): length=float(input("length")) list_.append(length) return list_ def __dispSides__(self,list_): for i in list_: print("side length",i) stone= polygon() stone.__dispSides__(4)  but then in the second to last line i was getting an error that said : Traceback (most recent call last): File "python", line 23, in TypeError: __init__() missing 1 required positional argument: 'x'  I was wondering how can i fix this and why I am getting this error? thanks!
Montmons
2#
 This is a quite straight-forward question. Whenever you call your polygon class its __init__ function is run automatically. You've set this initiation function to require 2 arguments: self & x. Whenever you initiate your class you will thus have to provide your class with an x argument. Meaning instead of declaring stone = polygon(), you should declare something like stone = polygon(x). Furthermore, your reason for passing and initiating the x argument is unclear since no matter what value x is given, it is never passed along to any method within your class. This is because after declaring self.x = x, it is immediately overwritten to be an empty list by your self.x=[] declaration (leaving the actual x-argument unused).
chepner
3#
 A proper class wouldn't prompt the user for the side lengths; the code that creates a Polygon should be responsible for the I/O and simply pass the values as arguments to Polygon.__init__. class Polygon(object): def __init__(self, sides): self.sides = sides def display_sides(self): for s in self.sides: print("side length", s) numSides = int(input("number of sides")) sides = [] for i in range(numSides): length = float(input("length")) sides.append(length) s = Polygon(sides) s.display_sides() 
 In same code, I applied two fixes.Hope you wanted the same result. class polygon(object): def __init__(self,x=[]): #useless code self.x=x # force user to input numSide and length numSides=int(input("number of sides")) list_=[] for i in range(numSides): length=float(input("length")) list_.append(length) #finally list_ argument is a declaration so you have to define it(assign value) def __dispSides__(self,list_=list_): for i in list_: print("side length",i) stone= polygon() stone.__dispSides__()  fix 1: __init__(self,x=[]): define x with declaration to set a default value fix 2: def __dispSides__(self,list_=list_): here you declared a variabe list_ but actually you wanted to pass previous list_ value here(as it seems ) so do pass it a value. Avoid Error: stone.__dispSides__(4)# passing a non-iterable will raise an error