Question

In: Computer Science

1. Number of Kids Type all the numbers from the whiteboard into a data file n_kids.txt,...

1. Number of Kids

Type all the numbers from the whiteboard into a data file n_kids.txt, one number
per line.

Write a program n_kids.py that reads the data from n_kids.txt, does some math, then displays the following:

Total number of families:
Total number of kids:
Average number of kids per family: (to 2 dp)
Maximum number of kids in a family:
Minimum number of kids in a family:

Write the above results to a different file (results.txt, not to the
input file n_kids.txt) in addition to displaying them on the screen. Do not use built in min and max functions.

Whiteboard Data:
4
2
2
2
4
1
1
2
4
4
3
3
3
2
2
3
1
2
3
2
2
3
2
2
2
4

Solutions

Expert Solution

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:

=====


Related Solutions

Implement a function that reads numbers in from a file with one number per line and...
Implement a function that reads numbers in from a file with one number per line and outputs all the possible sums that can be formed by subsets of the numbers. For instance, if the numbers in the file are 1 2 4, then the output would be 0, 1, 2, 4, 3, 5, 6, 7. Note that 0 is in the output because it uses none of the numbers, while 7 is the sum of all of the numbers. //...
Write a php program that writes numbers to a file. The file type should be .txt...
Write a php program that writes numbers to a file. The file type should be .txt and the file name should be numbers.tx. The numbers.txt should contain 10 random integers between 1 to 100 after the file is written.
4. Write a program that reads all numbers from a file and determines the highest and...
4. Write a program that reads all numbers from a file and determines the highest and lowest numbers. You must NOT use arrays to solve this problem! Write functions where appropriate. Programming language should be C
In the Excel data file, the tab labeled Question 1 contains data on the number of...
In the Excel data file, the tab labeled Question 1 contains data on the number of times boys and girls raise their hands in class. Conduct the t-test: Two-Sample Assuming Equal Variances. Males 9,8,4,9,3,8,10,8,9,10,7,6,12 Females 3,5,1,2,6,4,3,6,7,9,7,3,7,6,8,8 a. What is the null hypothesis? b. What is the research hypothesis? c. Why run a Two-Sample Assuming Equal Variances t-test? d. Interpret the findings. What are the results of the hypothesis test? Can you reject the null hypothesis?
Write a program that takes its input from a file of number type double and outputs...
Write a program that takes its input from a file of number type double and outputs the average of the numbers in the file to the screen. The file contains nothing but numbers of the type double separated by blanks and/ or line breaks. If this is being done as a class assignment, obtain the file name from your instructor. File name: pr01hw05input.txt 78.0 87.5 98.1 101.0 4.3 17.2 78.0 14.5 29.6 10.2 14.2 60.7 78.3 89.3 29.1 102.3 54.1...
C ++ Data File Preparation 1. Using the original AL Weather Station Data file find all...
C ++ Data File Preparation 1. Using the original AL Weather Station Data file find all records that have a bad data flag (-9999) for either the PRCP, TMAX or TMIN fields. Produce a new data file (call it Filtered_AL_Weather_Station.txt ) that omits those records with bad data flags. This new file will be used in problem 2. NOTE: The temperatures are given in tenths of a degree Celsius. e.g 83 is 8.3 degrees C.    2. Using the filtered data...
For the following data, determine the correct data type. 1- The number of protesters at a...
For the following data, determine the correct data type. 1- The number of protesters at a rally against a new government initiative. Qualitative Quantitative (Discrete) Quantitative (Continuous) 2- The height of buildings in a city. Qualitative Quantitative (Discrete) Quantitative (Continuous) 3- The distance from your home to school. Nominal Ordinal Interval Ratio 4- The following table displays some data about the top scoring NHL (National Hockey League) players in the 2017-2018 season, ranked by total points scored. Player Rank Age...
1. Change all of the numbers in the data area of your worksheet so that it...
1. Change all of the numbers in the data area of your worksheet so that it looks like this: Data Selling price per unit $292 Manufacturing costs: Variable per unit produced: Direct materials $125 Direct labor $55 Variable manufacturing overhead $23 Fixed manufacturing overhead per year $172,800 Selling and administrative expenses: Variable per unit sold $7 Fixed per year $74,000 Year 1 Year 2 Units in beginning inventory 0 Units produced during the year 3,200 2,700 Units sold during the...
A triangular number is the sum of the n natural numbers from 1 to n. For...
A triangular number is the sum of the n natural numbers from 1 to n. For example: The triangular number for 3 is 1 + 2 + 3 = 6 The triangular number for 7 is 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28 Write a program segment (using a loop), that calculates and then prints the integer n and its triangular number.
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type...
Using Java Project 2: Deduplication Write a program that reads a file of numbers of type int and outputs all of those numbers to another file, but without any duplicate numbers. You should assume that the input file is sorted from smallest to largest with one number on each line. After the program is run, the output file should contain all numbers that are in the original file, but no number should appear more than once. The numbers in the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT