Can someone correct this lines of code . Using Python in ERPNEXT

#Initialization
{%- sum = 0 -%}

#Loop getting the values from amount column in loan disbursement charges table
{% set loan_disbursement_charges = doc.loan_disbursement_charges %}
{%- for row in loan_disbursement_charges -%}

   #Getting the total of the values of amount rows      
   {%- sum += row.get_formatted("amount", doc) -%}<br>

{%- endfor -%}

#Getting the difference , placing it to total variable
{%- total = sum - doc.disbursed_amount -%}

#Printing total
{%- print(total) -%}

This is just a guess assuming the code is supposed to be valid python.

Try this

{% loan_disbursement_charges = doc.loan_disbursement_charges %}

I had to guess because you dud not share i formation on the error you saw.

{% set sum = 0 %}
{% set loan_disbursement_charges = doc.loan_disbursement_charges %}
{% for row in loan_disbursement_charges %}
{% set sum = sum + row.amount %}
{% endfor %}
{% set total = sum - doc.disbursed_amount %}
{{ total }}

i revised the code like this … no more error , but the arithmetic is not reflecting to the output

This does not seem to be a python code issue.
Are you sure you are asking for help in the right place?