A Personal Fitness Tracker is a wearable device that tracks your physical activity, calories burned, heart rate, sleeping patterns, and so on. One common physical activity that most of these devices track is the number of steps you take each day. The steps.txt file, available below, 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.) Your program should use constants for the number of days in each month. Your program should use a function to calculate the average number of steps. You will need to pass more than one argument to the function.
The steps to we need to solve this problem are below. Use the indentation as a hint to the indentation for python.
Display introduce message to the program
Open the files using the open command
Display the heading for the table we will build
Create a loop to loop through the 12 months
For the given month, call a function to lookup the number of days in that month
For the given month, call a function to lookup the string name of that month
Call a function to calculate the average steps taken for that month
Create a loop to loop through the number of days in that month
Read one line from the file
Add the number of steps taken on that day to a running total
Calculate the average steps taken = running total / days in month
Print the month name and the average
Display program ending message
Make sure you create constants for this program.
FILENAME = "steps.txt"
READ_MODE = "r"
NUMBER_OF_MONTHS = 12
JANUARAY = 1
DAYS_IN_JAN = 31
DAYS_IN_FEB = 28
DAYS_IN_MAR = 31
DAYS_IN_APR = 30
DAYS_IN_MAY = 31
DAYS_IN_JUN = 30
DAYS_IN_JUL = 31
DAYS_IN_AUG = 31
DAYS_IN_SEP = 30
DAYS_IN_OCT = 31
DAYS_IN_NOV = 30
DAYS_IN_DEC = 31
he file entries are in chronological order starting on January 1st and end in with December 31st .
The first loop goes through the months. The constants defined above this loop is represented with the following for statement.
for month in range(JANUARAY, NUMBER_OF_MONTHS + 1):
Then inside the loop we can call two functions.
The first function also takes the month as an argument and returns the name of the month as a string. This function is another big if statement that converts the integer month to the string name. Your function call will look like this.
month_name = get_name_of_month(month)
The second function takes the month as an argument and returns the number of days in that month. This function is a big if statement that determines the number of days in the month based on the integer month. Your function call will look like this.
days_in_month = get_day_in_month(month)
These two functions are very similar in that they take an integer representation of the month as a parameter. The difference is what data is returned. We could combine these functions, but we want to make the function well-defined with a single purpose. Therefore, we will keep them separated. A call these functions lookup because we apply an integer like 5, and we "lookup" the return value.
We can implement the lookup table with large if/elif/else statement as shown below.
if (numeric_month == JANUARAY):
month_name = "Jan"
elif (numeric_month == FEBRURAY):
month_name = "Feb"
elif (numeric_month == MARCH):
month_name = "Mar"
elif (numeric_month == APRIL):
month_name = "Apr"
… and so on
With the number of days per month, we can make our third function call which reads the correct number of lines from the file based on the number of days in the month. This function call will look like this.
average_steps = calc_average_steps(in_file, days_in_month)
Inside of the function calc_average_steps, we have another for loop that goes through the days of the month (days_in_month). For each iteration of the loop, you do a readline() to read one line from the file. Convert the line to an integer and then add it to a running sum of steps take for that month. The loop looks like this
for day in range(day_in_month):
total_num_steps += int(file.readline())
Once the loop ends, divide the running sum by the number of days to get the average and return the average.
Unfortunately, I am unable to post the steps.txt here but I am having issues creating the functions my professor requires. This has to be in Python.