Skip to content

Instantly share code, notes, and snippets.

@Sanae6
Created November 19, 2025 19:28
Show Gist options
  • Select an option

  • Save Sanae6/6504ca9403ff867a76a68fb5a7a55a4d to your computer and use it in GitHub Desktop.

Select an option

Save Sanae6/6504ca9403ff867a76a68fb5a7a55a4d to your computer and use it in GitHub Desktop.
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()
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