How to write a program in Python that allows to write common fraction in the form of a finite simple continued fraction?

I recently started to learn Python. On one of the online courses I came across the following problem(see picture)


The actual decomposition fraction is sufficiently detailed and clearly described in Wikipedia (Непрерывная_дробь)

I wrote the following program:
while b!=0:

While trying to test the example from the job, the required coefficients (7,1,29) are displayed correctly, but the loop becomes infinite and for some reason does not stop, despite the fact that at some point b becomes zero. I, frankly, broke his head in an attempt to understand why this is happening.. maybe someone can explain to me, what am I missing?

I wrote a program using divmod, but, similarly, although the equality of b to zero, the loop continues( and the list of coefficients instead of 29 turns 30..

In General, I would be very grateful if someone will help with advice a beginner)
March 19th 20 at 09:01
1 answer
March 19th 20 at 09:03
Do not use float division. Keep a pair (numerator, denominator) to represent the value of the "tail". In the end, you can only do integer division and taking the remainder.

num, den = (int(v) for v in input().split('/'))
a, q = divmod(num, den)
t = den #q/t -- tail
res = [a]
while q != 0:
 next_t = q
 a, q = divmod(t, q)
 t = next_t
Res. append(a)
Thank you very much! Figured out finally)
but float division is a dangerous thing, as it turned out.. - joel67 commented on March 19th 20 at 09:06

Find more questions by tags Python