[Tutor] fractions from Fractions
Rex Florian
sunnlotus at aol.com
Sun Feb 4 23:11:23 EST 2018
I have written a program to generate the array that describes the finite simple continued fraction from any fractional input. The input is any fraction, num/den, and the output is the array. The program works and I am happy with the work.
The problem is one of the PyCharm problems and when I use the check feature it tells me my answer is incorrect. I think I know the source of the trouble. The problem asks that my input format be "A single line with an fraction in the numerator/denominator format.
My input is obtained as seen in the code. I have googled fractions in Python. I have tried importing fractions from Fraction but seem to be in over my head. What am I missing?
fraction = input()
# define list for holding coefficients
coef = []
# find the index where the / is
slash = fraction.find('/')
# extract the string num and den and convert them to integers
num = int(fraction[0:slash])
den = int(fraction[slash + 1:])
while num > den and den != 0:
mod = num % den
a = num // den
coef.append(a)
num = den
den = mod
for i in coef:
print(i, end=' ')
More information about the Tutor
mailing list