简体   繁体   中英

how can i create a class inside a loop

Experiment

class calculator:
    class add:
        def __init__(self,*arg):
            self.arg = arg
            
        def display_new(self):
            return sum(self.arg)
            
    class multiply:
        def __init__(self,*arg):
            self.arg = arg
        
        
        class multi_num:
            def __init__(self,*arg):
                self.arg = arg
            
            def nature(self):
                sum = 1
                for x in arg:
                    sum=sum*x
                return sum
      
    class devide:
        def __init__(self,x,y):
            self.x = x
            self.y = y
            
        def display3(self):
            try:
                result = self.x/self.y
            except ZeroDivisionError:
                print("error: divided by zero")
            else:
                return (result)
            
calc = calculator()

     multiply = calc.multiply(1,2,3,4,5)
     multiply.display2()

its not displaying the result. how can I fix this and where am I making mistake,add and divide is working properly but I don't know why this multiply is not working.

Change arg to self.arg :

 def nature(self):
                sum = 1
                for x in self.arg:
                    sum=sum*x
                return sum

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM