In: Computer Science
Suppose that the file inData.txt contains the following data:
10.20 5.35 15.6 Randy Gill 31 18500 3.5 A |
The numbers in the first line represent the length and width, respectively, of a rectangle. The number in the second line represents the radius of a circle. The third line contains the first name, last name, and the age of a person. The first number in the fourth line is the savings account balance at the beginning of the month, and the second number is the interest rate per year. (Assume that π = 3.1416.) The fifth line contains an uppercase letter between A and Y (inclusive). Write statements so that after the program executes, the contents of the file outData.txt are as shown below. If necessary, declare additional variables. Your statements should be general enough so that if the content of the input file changes and the program is run again (without editing and recompiling), it outputs the appropriate results.
Rectangle: Length = 10.20, width = 5.35, area = 54.57, parameter = 31.10 Circle: Radius = 15.60, area = 764.54, circumference = 98.02 Name: Randy Gill, age: 31 Beginning balance = $18500.00, interest rate = 3.50 Balance at the end of the month = $18553.96 The character that comes after A in the ASCII set is B |
f=open("inData.txt",'r')
p=open("outData.txt",'a')
p.write("Rectangle:\n")
index=0
pi=3.1416
for line in f.readlines():
if(index==0):
l,w=line.strip().split(" ")
l=float(l)
w=float(w)
area=round(l*w,2)
parameter=round(2*(l+w),2)
p.write("Length = "+str(l)+", width = "+str(w)+", area = "+str(area)+", parameter = "+str(parameter)+" Circle.\n")
elif(index==1):
r=line.strip()
r=float(r)
area=round(pi*r*r,2)
circumference=round(2*pi*r,2)
p.write("Radius = "+str(r)+", area ="+str(area)+", circumference ="+str(circumference)+"\n")
elif(index==2):
fname,lname,age=line.strip().split(" ")
p.write("Name:{} {}, age:{}\n".format(fname,lname,age))
elif(index==3):
balance,interest=line.strip().split(" ")
balance=float(balance)
interest=float(interest)
value=((interest/100*balance)/12)+balance
p.write("Beginning balance=${0:.2f},interest rate={1:.2f}\n".format(balance,interest))
p.write("Balance at the end of the month=${:0.2f}\n".format(value))
elif(index==4):
char=line.strip()
a=ord(char)
p.write("The character that comes after {} in the ASCII set is {}".format(char,chr(a+1)))
index=index+1
#output: