Upon completion of this assignment, students should be able to:
- Open and close files
- Read from, write to, and update files
- Manipulate file handles
- Apply standard library functions:
open(),close(),read(),write() - Compose decision statements (
ifconditional statements) - Create and utilize compound conditions
Before starting this assignment, students should be able to:
- Analyze a basic set of requirements and apply top-down design principles
- Define and use Python functions
- Implement guard code using
if __name__ == "__main__": - Summarize topics including:
- What is a selection or conditional statement?
- What is a compound condition?
- What is a Boolean expression?
Write a Python program that processes numbers corresponding to student records read from a file and writes the required results to an output file.
Implement the following functions:
read_student_record(file): Reads a line from the input file and returns a tuple(gpa, class_standing, age)as floats/ints.calculate_sum(numbers): Returns the sum of the provided numbers (accepts a list of numeric arguments). Returns -1.0 if the list is empty.calculate_mean(numbers): Returns the mean of the numbers. Returns -1.0 if the list is empty.calculate_deviation(number, mean): Returns the deviation asnumber - mean.calculate_variance(numbers, mean): Returns the variance as the average of squared deviations from the mean. Returns -1.0 if the list is empty.calculate_standard_deviation(variance): Returns the standard deviation as the square root of the variance.find_max(numbers): Returns the maximum value in a list of numbersfind_min(numbers): Returns the minimum value in a list of numbersprint_float(file, number, decimals=2): Writes a floating-point number to the output file (expects a file object opened for writing), formatted to the specified number of decimal places (default is 2).
The main() function should:
- Open an input file named
input.csvfor reading. - Open an output file named
output.datfor writing. - Read five student records from
input.csv. Each record includes:- GPA
- Class standing
- Age
- Calculate the sum and mean for GPAs, class standings, and ages.
- Calculate the deviation of each GPA from the mean GPA.
- Calculate the variance and standard deviation of the GPAs.
- Determine the minimum and maximum GPA values.
- Write the results to
output.dat. - Close both the input and output files.
The input file input.dat should contain five student records, each on a separate line with the following format:
<GPA> <ClassStanding> <Age>
Example:
3.5 2 20
3.8 3 21
2.9 1 19
3.2 4 22
3.7 2 20
The output file output.dat should contain the calculated statistics with appropriate labels.
Example:
Mean GPA: 3.42
Mean Class Standing: 2.4
Mean Age: 20.4
GPA Standard Deviation: 0.34
Minimum GPA: 2.9
Maximum GPA: 3.8
- Submit your solution by pushing your code to the GitHub Classroom repository provided for this assignment.
- Make sure all your code, including
main.py, yourREADME.md, and any other required files, are committed and pushed to your GitHub Classroom repository. - Ensure your submission includes all necessary files to run your program and that your code runs as expected from the repository.