In: Computer Science
How do I make this code print the st variable rounded to 2 decimal places? Everything I have tried gives me an error. print('Enter output filename') name=input() print('Enter principal amount') p=float(input()) print('Enter term length (month)') n=int(input()) print('Enter annual interest') i=float(input()) j=i/12 m=p*j/(1-(1+j)**-n) file = open(name, "w") title = ["Month, ", "Total Accured interest, ", "Loan Balance \n"] file.writelines(title) first_row=["0",",","$","0.00",",","$",p," \n"] s1=' '.join([str(elem) for elem in first_row]) file.writelines(s1) t=0 for j in range(n): tai=(p*i/12) t=t+tai p=(p-m)+tai st="" ls=[j+1,",","$",t,",","$",p,"\n"] st=''.join([str(elem) for elem in ls]) file.writelines(st) file.close()
Code Screenshot :
Executable Code:
print('Enter output filename')
name=input()
print('Enter principal amount')
p=float(input())
print('Enter term length (month)')
n=int(input())
print('Enter annual interest')
i=float(input())
j=i/12
m=p*j/(1-(1+j)**-n)
file = open(name, "w")
title = ["Month, ", "Total Accured interest, ", "Loan Balance
\n"]
file.writelines(title)
first_row=["0",",","$","0.00",",","$",p," \n"]
s1=' '.join([str(elem) for elem in first_row])
file.writelines(s1)
t=0
for j in range(n):
tai=(p*i/12)
t=round(t+tai,2)
p=round((p-m)+tai,2)
st=""
ls=[j+1,",","$",t,",","$",p,"\n"]
st=''.join([str(elem) for elem in ls])
file.writelines(st)
file.close()
Sample Output :
file.txt
Please comment
below if you have any queries.
Please do give a thumbs up if you liked the answer thanks
:)