Stuck on a python program
2 posters
Page 1 of 1
Stuck on a python program
My main objective is to add the division and multiplying of fractions and have them reduce if needed. My multiplying works but doesn't reduce and I can't get the division to work using ( / ) as the dividing sign.
UPDATED CODE:
I changed some orders and got the addition to work and find the GCD, but not my multiplying doesn't work.
my output:
3/4
Traceback (most recent call last):
File "C:\Python32\TextBook Code\Fraction.py", line 50, in <module>
f3 = (f1 * f2)
TypeError: unsupported operand type(s) for *: 'Fraction' and 'Fraction'
- Code:
#Fraction.py
import fractions
import math
def gcd(m,n):
while m%n != 0:
oldm = m
oldn = n
m = oldn
n = oldm%oldn
return n
class Fraction:
def __init__(self,top,bottom):
self.num = top
self.den = bottom
def __str__(self):
return str(self.num)+"/"+str(self.den)
def show(self):
print(self.num,"/",self.den)
def __mul__(self,other):
assert type(other) == Fraction
top = self.num*other.num
bot = self.den*other.den
return Fraction(top,bot)
x = Fraction(3,8)
y = Fraction(6,9)
print(x * y)
print(x == y)
def __div__(self,other):
assert type(other) == Fraction
top = self.num*other.den
bot = self.den*other.num
return Fraction(top,bot)
def __add__(self,other):
newnum = self.num*other.den + \
self.den*other.num
newden = self.den * other.den
common = gcd(newnum,newden)
return Fraction(newnum//common,newden//common)
def __eq__(self,other):
firstnum = self.num * other.den
secondnum = other.num * self.den
return firstnum == secondnum
UPDATED CODE:
I changed some orders and got the addition to work and find the GCD, but not my multiplying doesn't work.
my output:
3/4
Traceback (most recent call last):
File "C:\Python32\TextBook Code\Fraction.py", line 50, in <module>
f3 = (f1 * f2)
TypeError: unsupported operand type(s) for *: 'Fraction' and 'Fraction'
- Code:
#Fraction.py
import fractions
import math
def gcd(m,n):
while m%n != 0:
oldm = m
oldn = n
m = oldn
n = oldm%oldn
return n
class Fraction:
def __init__(self,top,bottom):
self.num = top
self.den = bottom
def __str__(self):
return str(self.num)+"/"+str(self.den)
def show(self):
print(self.num,"/",self.den)
def __add__(self,other):
assert type(other) == Fraction
newnum = self.num*other.den + \
self.den*other.num
newden = self.den * other.den
common = gcd(newnum,newden)
return Fraction(newnum//common,newden//common)
f1 = Fraction(1,4)
f2 = Fraction(1,2)
f3 = (f1 + f2)
print (f3)
def __mul__(self,other):
assert type(other) == Fraction
newnum = self.num*other.num
newden = self.den*other.den
common = gcd(newnum,newden)
return Fraction(newnum//common,newden//common)
f4 = Fraction(3,8)
f5 = Fraction(6,9)
f6 = (f4 * f5)
print (f6)
def __div__(self,other):
assert type(other) == Fraction
newnum = self.num*other.den
newden = self.den*other.num
common = gcd(newnum,newden)
return Fraction(newnum//common,newden//common)
f8 = Fraction(3,8)
f9 = Fraction(6,9)
F10 = (f8 / f9)
print (f10)
Pibor- Tier 4 (500 posts)
Re: Stuck on a python program
I'm lost are you trying to check if it divides evenly or what?
Gecko- Tier 4 (500 posts)
Similar topics
» Python code
» Python Vacuum Robot Script
» Stuck at 1-5M? ~~Must Read~~
» Why Learn to Program?
» Need someone to code a program for me in C#
» Python Vacuum Robot Script
» Stuck at 1-5M? ~~Must Read~~
» Why Learn to Program?
» Need someone to code a program for me in C#
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|