Python学习笔记:创建分数类
1、编写创建分数类.py
# 创建分数类
from math import gcd
# 定义分数类
class Fraction:
def __init__(self, top, bottom):
if type(top) == int and type(bottom) == int:
common = gcd(top, bottom)
self.num = top // common
self.den = bottom // common
else:
raise ValueError('错误:分子或分母不是整数!')
def getNum(self):
return self.num
def getDen(self):
return self.den
def show(self):
print(self.num, "/", self.den)
def __str__(self):
re