In: Computer Science
Implemented the code as per the requirement. As python is
indentation specific, you may not get the formatted text while
copying the code,
so I'm attaching the screenshots of the code for reference. Please
make sure when you are executing the below code you have same
format, especially tabs.
Please comment if any modification required or if you need any help.
Code:
====
fil1 = open("F:\\Eclipse_Workspace\\FirstProject\\n_kids.txt", "r+") total_families=0 total_kids=0 firstline = fil1.readline().strip() total_families = total_families+1 total_kids = total_kids+int(firstline) min = int(firstline) max = int(firstline) for line in fil1: total_families = total_families+1 number = int(line.strip()) total_kids = total_kids+ number if(min>number): min = number if(max<number): max = number file_out = open("F:\\Eclipse_Workspace\\FirstProject\\results.txt","w+") file_out.write("Total number of families: "+str(total_families)+"\n") print("Total number of families: "+str(total_families)) file_out.write("Total number of kids: "+str(total_kids)+"\n") print("Total number of kids: "+str(total_kids)) file_out.write("Average number of kids per family: "+str(round((total_kids/total_families),2))+"\n") print("Average number of kids per family: "+str(round((total_kids/total_families),2))) file_out.write("Maximum number of kids in a family: "+str(max)+"\n") print("Maximum number of kids in a family: "+str(max)) file_out.write("Minimum number of kids in a family: "+str(min)+"\n") print("Minimum number of kids in a family: "+str(min))
code screenshot:
=============
Output:
=====