dimanche 26 septembre 2021

How to skip multiple times call a function in class?

I designed a class named Funcskip. I want to randomly call function which are defined into the class. But I also want to skip functions which are called already. How can I do this from following code.

import random
class Funcskip:
    def __init__(self,
                 min_ops=2,
                 max_ops=4):
      self.min_ops                =   min_ops
      self.max_ops                =   max_ops
      self.ops              =   [self.__add, self.__sub, self.__mult, self.__div]

    def __initParams(self):
      self.num_ops          =    random.randint(self.min_ops,self.max_ops)

    def __add(self, a, b):
      print("1. add")
      return a+b
    def __sub(self, a,b):
      print("2. sub")
      return a-b

    def __mult(self, a, b):
      print("3. mult")
      return a*b
    def __div(self, a,b):
      print("4. div")
      return a//b

    def callF(self,a,b):
        self.__initParams()
        print(self.num_ops)
        total = 0
        for _ in range(self.num_ops):
            total+=random.choice(self.ops)(a,b)
        return total

    

  
obj = Funcskip()
a = 10
b = 5
_total = obj.callF(a,b)
print(_total)
                 



Aucun commentaire:

Enregistrer un commentaire