def filter_terms(p, degree_limit):
    """
    This function gets the n-variable polynomial p and the number degree_limit
    and returns the polynomial which consists of terms of degree at most degree_limit
    :param p: n-variable polynomial
    :param degree_limit: boundary for degree
    :return: n-variable polynomial with terms of degree at most limit
    """
    for m in p.monomials():
        if m.degree() > degree_limit:
            p -= m*p.monomial_coefficient(m)
    return p