In Python:
file = open('steps.txt', 'r')
readLine = file.readlines()
begin = 0
mdays = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
print("Month"+"\t\t"+"Average Steps")
for m in range(len(mdays)):
end = begin + mdays[m]
steps = readLine[begin:end]
sumsteps = 0
for s in steps:
sumsteps = sumsteps + int(s)
average = round(sumsteps/mdays[m],1)
print(str(m+1)+"\t\t"+str(average))
begin = begin + mdays[m]
Explanation:
This line opens the step.txt file
file = open('steps.txt', 'r')
This reads the content of the file
readLine = file.readlines()
begin = 0
This initializes the months of the days to a tuple
Mmdays = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
This prints the header
print("Month"+"\t\t"+"Average Steps")
This iterates through the tuple
for m in range(len(Mmdays)):
This calculates the days in each month
end = begin + Mmdays[m]
This gets the number of steps in each month
steps = readLine[begin:end]
This initializes the number of steps to 0
sumsteps = 0
This iteration sums up the number of steps in each month
for s in steps:
sumsteps = sumsteps + int(s)
This calculates the average of steps rounded to 1 decimal place
average = round(sumsteps/Mmdays[m],1)
This prints the calculated average
print(str(m+1)+"\t\t"+str(average))
This calculates the beginning of the new month
begin = begin + Mmdays[m]