170k views
0 votes
# Name: Your Name # Class and Section: CS 120 01 # Assignment: Chapter 06 # Due Date: See above # Date Turned in: # Program Description: This program reads a file containing the number of steps taken each day for a year and calculates the average number of steps for each month. It also finds the highest and lowest step days. def read_file(filename): """ Reads the steps.txt file and returns a list of steps for each day. """ try: with open(filename, 'r') as file: steps = [int(line.strip()) for line in file] return steps except IOError: print("Error reading file:", filename) return [] def calculate_average(steps): """ Calculates the average number of steps for each month and returns a list of averages. """ averages = [] months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] total_steps = 0 days = 0 month_index = 0 for i in range(len(steps)): total_steps += steps[i] days += 1 if days == days_in_month[month_index]: average = total_steps / days averages.append((months[month_index], average)) total_steps = 0 days = 0 month_index += 1 return averages def find_highest_lowest_steps(steps): """ Finds the highest and lowest step days and returns a tuple containing the day number and the number of steps. """ highest_day = 1 highest_steps = steps[0] lowest_day = 1 lowest_steps = steps[0] for i in range(1, len(steps)): if steps[i] > highest_steps: highest_day = i + 1 highest_steps = steps[i] elif steps[i] < lowest_steps: lowest_day = i + 1 lowest_steps = steps[i] return (highest_day, highest_steps), (lowest_day, lowest_steps) def display_results(averages, highest_lowest_steps): """ Dis

1 Answer

5 votes

The program reads a file containing daily steps and calculates the average steps per month, as well as the highest and lowest step days.

The program provided reads a file containing the number of steps taken each day for a year and calculates the average number of steps for each month. It also finds the highest and lowest step days.

The program uses three functions: read_file() to read the file and return a list of steps, calculate_average() to calculate the average number of steps for each month, and find_highest_lowest_steps() to find the highest and lowest step days.

The main function is display_results() which takes the results from the previous functions and displays them.

The probable question may be:

Explain the logic behind the calculate_average() function. How does it calculate the average number of steps for each month?

User Leang Socheat
by
8.5k points