In: Computer Science
Using Python 3
Write a function reads the file called simpleinterest.txt. Each row in simpleinterest.txt is a comma seperated list of values in the order of PV, FV, n, r.
For each row, there is one value missing.
Write an output file that fills in the missing value for each row.
code:
import math
res=[]
with open('simpleinterest.txt') as f:
for temp in f:
line=temp.strip().split(",")
if line[1]=='':
PV=float(line[0])
n=float(line[2])
r=float(line[3])
line[1]=str(PV*(1+r*n))
elif line[0]=='':
FV=float(line[1])
n=float(line[2])
r=float(line[3])
line[0]=str(FV/(1+r*n))
elif line[2]=='':
FV=float(line[1])
PV=float(line[0])
r=float(line[3])
line[2]=str(((1/((FV/PV)-1))*r))
else:
FV=float(line[1])
PV=float(line[0])
n=float(line[2])
line[3]=str(((FV/PV)-1)*n)
res.append(line)
with open('simpleinterest.txt','w') as f:
for line in res:
text=''
for string in line:
text=text+string+','
f.write(text+'\n')
Before execution of code:
contents of simpleintrest.txt:
100,200,,2
100,,2,2
100,200,2,
,200,2,2
After execution of code:
contents of simpleintrest.txt:
100,200,2.0,2,
100,500.0,2,2,
100,200,2,2.0,
40.0,200,2,2,
Sample reference for indendation:
please upvote if you like the answer. Thank you