class Faction(object): def __init__(self,fz=0,fm=1): self.__fz = fz if fm == 0: raise ValueError('分母不能为0') else: self.__fm =fm @property def fz(self): return self.__fz @property def fm(self): return self.__fm # 将分子和分母转换成分数格式 def chang_format(self): min_value = min(self.__fz,self.__fm) max_value = max(self.__fz,self.__fm) while max_value % min_value != 0: temp = max_value%min_value max_value = min_value min_value = temp self.__fz //= min_value self.__fm //= min_value if self.__fm == 1: str = "{fz}".format(fz=self.__fz) else: str ="{fz}/{fm}".format(fz=self.__fz, fm=self.__fm) return str # 定义一个计算类,专门进行分数的加,减