|   | 1 |  initial version  | 
In Python (the programming language Sage uses),
=, while == is to test equalitywhile loop is everything that is indented after :.So your code might want to look something like that instead:
def div(f, g):
    q = 0
    r = f
    while r != 0 and g.lt().divides(r.lt()):
        q = q + r.lt() // g.lt()
        r = r - (r.lt() // g.lt()) * g
    return q, r
Then you can use the function as follows.
Define a polynomial ring and two polynomials:
sage: R.<x> = QQ[]
sage: f = x^5 + x^3 + 2
sage: g = x^2 + 1
Apply the function:
sage: div(f, g)
(x^3, 2)
 Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.
 
                
                Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.