Please do rate
the answer thanks,..
Executable Code:
---------------------------------------------------------------------------------------------------
# function to find the factors of a number
def factors(n):
# empty list
t=[]
# looping
for i in range(1,n+1):
# if n is divisible
by i , then i is a factor of n
if n % i == 0:
t.append(i)
# returning the list
return t
# function to return the fraction of a decimal number
def fraction(n):
# counter
mul_count=0
# converting the decimal number to
string
s=str(n)
# temporary variable
t=0
# finding the index of '.'
index=s.index('.')
# looping
while s[index+1]!='0':
# converting string
to float
t=float(s)
# multiplying the
number by 10
t=t*10
# incrementing the
counter
mul_count+=1
# converting the
number to string
s=str(t)
# finding the index
of '.'
index=s.index('.')
# getting the numerator
numerator=int(t)
# variable for denominator
denominator= 1
# finding the denomiantor
for i in range(mul_count):
denominator*=10
# finding the factors numerator and
denomiantor
f_numerator=factors(numerator)
f_denominator=factors(denominator)
# finding the gcf of two list
a=set(f_numerator)
b=set(f_denominator)
gcf=max( a & b)
# returning the fraction
return
(int(numerator/gcf),int(denominator/gcf))
# testing
if __name__=='__main__':
print(fraction(0.750))
print(fraction(0.5))
print(fraction(0.250))
------------------------------------------------------------------------------------------------------
SCREENSHOT FOR OUTPUT:
python 3 Converting a decimal to a fraction 10 points Although computers generally represent numbers as...