From b2fe8a53f41347e7f77f73f547ef72e02474d994 Mon Sep 17 00:00:00 2001 From: Sirin Puenggun Date: Fri, 25 Nov 2022 20:58:05 +0700 Subject: [PATCH] Checking Minus sign and Check devide by 0 error --- .../__pycache__/fraction.cpython-310.pyc | Bin 0 -> 2961 bytes nessesary/fraction.py | 48 +++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) create mode 100644 nessesary/__pycache__/fraction.cpython-310.pyc diff --git a/nessesary/__pycache__/fraction.cpython-310.pyc b/nessesary/__pycache__/fraction.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..54e7f9e0fc1521583386177f7797812d57a78d7a GIT binary patch literal 2961 zcmcImPjBNy6rULDDE-!`Lor z)(flC%w(6NboL zk_+0-Lq9JHx?hm_(*?1Ratbgq$2_qpEZA#`B~gTSQItd(T1za83badNMHY{lsET`+ zxpqmcf>p02=3~|AhK}!b(VlU^K7;xN)SwL{rh@)JR5=Cp#q$RPp8BgzD_4D}lI7ucYj zs?mqLRfj6!?lO=XRj5x@kRSvZ>5DMDhf^y+GO{&dT~s+P*vG1U1f}RZ2G2hjYEfup zjp;Ka`7^w_S!H;X`%nXvW3egmIhabtH()SwGb=X0>5Po)F;$&*Gi?Iy1??zCpSG48 zqKWaT@RSFn{?3N|(F}hJ z_#os1+igv4>?_9$*WHY`*2kNzX1eCe$bf`ydX*pDq#vG0HDM+NC4;CJ&LEjAaNZGu z^LJqHHC}XaaSd?H(Lp6W>S)P-!**>Rw!fzbIf#ye5W_cxv61rrPlW3)N4Vb1BCK?n ztpYN0pbMfS&bN@a24o5{&AJ`3cVYb;cckE&H(l_z6pF9m3Y~uM{1v10{>xEnet2h; z+^EO-BRKHL!HPE>OfR_;T8L04c;}$?M~c>-bLI${VrBp2geSHR4BPkQt8uW+J3zO+ z+1yEuyxqZ;I1g1Mg>%OFH|S)T@&>D!xJ=m?-eIJ?8_!Cb|1hDV!##uF9n5{t$gc+E zH`dvPw2>O~E#{jEV2^#}{u@lq4ZAg+$wmpQIhloMO2z9)p3Q~z`64p2^_^K6m@mgT ztk)r-W>%w;n|br5jO|$&w-~ZLIZM`#7jzRIE=-xjEIa3!#_S4Njq~qem3}>|mFXmb z%>7y!|5m(?D>+Xr&Rt(bJ?s}b|1s+Hh8%@&U1GvtPhYC=e4;ri1TqXE+2b3n-azsu zlDBZZ0s7V3IGp@J=$`&b!50H-@DPYeOv*|YWd`K`Q8i6-*({soYFXjBefTHAcAl`r P{B&seu9k=odzJhHKo>^Z literal 0 HcmV?d00001 diff --git a/nessesary/fraction.py b/nessesary/fraction.py index a1da5e7..6209566 100644 --- a/nessesary/fraction.py +++ b/nessesary/fraction.py @@ -1,9 +1,38 @@ class Fraction: def __init__(self, numer, denom): - self.numer = numer - self.denom = denom + if numer < 0 and denom < 0: + numer = -numer + denom = -denom + if denom < 0: + numer = -numer + denom = -denom + if denom == 0: + raise ValueError("Denominator shouldn't be zero.") + self.__numer = numer + self.__denom = denom self.floating = numer/denom + @property + def numer(self): + return self.__numer + + @property + def denom(self): + return self.__denom + + @numer.setter + def numer(self, value): + self.__numer = value + + @denom.setter + def denom(self, value): + if denom < 0: + numer = -numer + denom = -denom + if denom == 0: + raise ValueError("Denominator shouldn't be zero.") + self.__denom = value + def __add__(self, other): """ Add Fraction. @@ -62,4 +91,19 @@ class Fraction: return result def __str__(self) -> str: + """ + Print Fraction Form. + >>> m1 = Fraction(1, 2) + >>> print(m1) + 1/2 + >>> m2 = Fraction(5, -2) + >>> print(m2) + -5/2 + >>> m3 = Fraction(-5, 2) + >>> print(m3) + -5/2 + >>> m4 = Fraction(-5, -2) + >>> print(m4) + 5/2 + """ return f"{self.numer}/{self.denom}" \ No newline at end of file