[Tutor] fractions from Fractions
Alan Gauld
alan.gauld at yahoo.co.uk
Mon Feb 5 04:47:20 EST 2018
On 05/02/18 04:11, Rex Florian via Tutor wrote:
> The problem is one of the PyCharm problems and when I
> use the check feature it tells me my answer is incorrect.
What input did you use and what output did you get?
So far as I can tell your algorithm is correct, although
I'm not sure why you limit it to values greater than 1?
But is it perhaps the format of the output that Pycharm
objects to?
> I think I know the source of the trouble.
Would you like to tell us?
> I have tried importing fractions from Fraction
And what happened?
(I assume you mean you tried importing Fraction from fractions)
Although you don't really need it for this problem.
> 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=' ')
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list