'step counter, cant get the output to match the projected output

The steps.txt file contains the number of steps a person has taken each day for a year. There are 365 lines in the file, and each line contains the number of steps taken during a day. (The first line is the number of steps taken on January 1st, the second line is the number of steps taken on January 2nd, and so forth.) Write a program that reads the file, then displays the average number of steps taken for each month. (The data is from a year that was not a leap year, so February has 28 days.

One of the months in the sample Run is not correct

Your program should handles the following exceptions:

  • It should handle any IOError exceptions that are raised when the file is opened and data is read from it.
  • It should handle any ValueError exceptions that are raised when the items that are read from the file are converted to a number.

Download the file using the following command:

cp $PUB/steps.txt .

Sample Run:

The average steps taken in January was 5,246.1
The average steps taken in February was 4,851.9
The average steps taken in March was 5,777.6
The average steps taken in April was 5,802.1
The average steps taken in May was 4,711.5
The average steps taken in June was 4,792.3
The average steps taken in July was 5,638.2
The average steps taken in August was 5,759.6
The average steps taken in September was 6,114.6
The average steps taken in October was 5,411.0
The average steps taken in November was 4,268.8
The average steps taken in December was 5,138.1
def main():
    try:
        step_count = open('steps.txt', 'r')

        content = step_count.read()
        
    except IOError:
        print('An error has occured trying to read file,')
              
    def steps(file, num):
        for count in range(1, num, 1):
            step = str(step_count.readline())
            total_steps = 0
            total_steps = str(total_steps) + (step)
            average = int(total_steps) / num
            format_average = "{:.2f}".format(average)
            return format_average





    print('The average steps taken in January was', steps(step_count, 31))
    print('The average steps taken in Febuary was', steps(step_count, 28))
    print('The average steps taken in March was', steps(step_count, 31))
    print('The average steps taken in April was', steps(step_count, 30))
    print('The average steps taken in May was', steps(step_count, 31))
    print('The average steps taken in June was', steps(step_count, 30))
    print('The average steps taken in July was', steps(step_count, 31))
    print('The average steps taken in August was', steps(step_count, 30))
    print('The average steps taken in September was', steps(step_count, 31))
    print('The average steps taken in October was', steps(step_count, 30))
    print('The average steps taken in November was', steps(step_count, 31))
    print('The average steps taken in December was', steps(step_count, 30))



    step_count.close()

main()

this is the output

The average steps taken in January was 0.00
The average steps taken in Febuary was 0.00
The average steps taken in March was 0.00
The average steps taken in April was 0.00
The average steps taken in May was 0.00
The average steps taken in June was 0.00
The average steps taken in July was 0.00
The average steps taken in August was 0.00
The average steps taken in September was 0.00
The average steps taken in October was 0.00
The average steps taken in November was 0.00
The average steps taken in December was 0.00

cant figure out how to get it to give me the right output


def steps(max_day):
    step_count = open('steps.txt', 'r')

    total_steps = 0.0
    for line in step_count:
        step = float(line)
        total_steps = (total_steps) + (step)
        average = (total_steps) / max_day
        format_average = "{:.2f}".format(average)
    return format_average
    
    step_count.close()

I came up with this, but its still not giving me the right out come

The average steps taken in January was 62089.87
The average steps taken in Febuary was 68742.36
The average steps taken in March was 62089.87
The average steps taken in April was 64159.53
The average steps taken in May was 62089.87
The average steps taken in June was 64159.53
The average steps taken in July was 62089.87
The average steps taken in August was 64159.53
The average steps taken in September was 62089.87
The average steps taken in October was 64159.53
The average steps taken in November was 62089.87
The average steps taken in December was 64159.53


Solution 1:[1]

def steps(max_days):
    total_steps = 0.0
    for count in range(max_days):
        step = float(step_count.readline())
        total_steps = total_steps + step
    average = total_steps / max_days
    format_average = "{:.2f}".format(average)
    return format_average

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 PCastedo