Created
November 19, 2025 19:28
-
-
Save Sanae6/6504ca9403ff867a76a68fb5a7a55a4d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def main(): | |
| infile = open("input.txt", 'r') | |
| outfile = open("output.txt", 'w') | |
| data = infile.readlines() | |
| # add a variable, name it count to keep track of how many numbers in the input file since it is used for average the total | |
| count = 0 | |
| # add code here to loop through the numbers | |
| numbers = list(map(float, data)) # far easier shorthand for a for loop that converts all data elements to floats | |
| count = len(numbers) # get count from for loop | |
| # add code here to sum up all the numbers | |
| num_sum = sum(numbers) | |
| # add code here to calculate the average of the numbers | |
| num_avg = num_sum / count | |
| #add code here to print the average of all numbers in the output file, format the average to 2 decimal points. | |
| print(f"The sum of all numbers is: {num_sum:.02f}", file=outfile) | |
| print(f"The average of all numbers is: {num_avg:.02f}", file=outfile) | |
| print("The sum and average of the numbers are in output.txt") | |
| infile.close() | |
| outfile.close() | |
| main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy | |
| a = numpy.loadtxt("MyData.csv", skiprows=5, delimiter=',') | |
| print(a) | |
| numpy.savetxt('output.txt', a, fmt="%.02f", delimiter=',') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment