Question

The steps.txt file contains the number of steps a person taken each day for a year....

The steps.txt file contains the number of steps a person 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 steps taken January 1st and the last line is the steps taken on December 31). Write a function that reads the file, then displays the total number of the steps in that year, and an average number of steps taken for each month (February is not a leap year in this data). Also, use try, except and finally suite to handle the errors: FileNotFoundError, TypeError, and ZeroDivisionError. (question was simplified to calculate average per month, which should be number of days times higher than shown below)

Should Return:

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

Please code in Python 3

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please note: As per question leap year case is not handled. code is for only 365 days only.

Output:

code:

months = ['JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER']

month_days_arr = [31,28,31,30,31,30,31,31,30,31,30,31]

try:

   current_month = 0

   current_month_days = month_days_arr[current_month]

   current_month_sum = 0

   file_handle = open('steps.txt','r')

   while True:

       for counter in range(current_month_days) :

           line = file_handle.readline()

           if not line:

               break

           current_month_sum += int(line)

       

       if not line:

            break

       

       try:

           print('The average steps taken in ' + months[current_month] + ' was ' + str(float(current_month_sum)/current_month_days))

       except ZeroDivisionError:

           print ('Skiping average steps taken in ' + months[current_month])

       

       if current_month < 11 :

            current_month += 1

            current_month_sum = 0

            current_month_days = month_days_arr[current_month]

except IOError:

   print ("Error: can\'t find file or read data")

finally:

   file_handle.close()

The average steps taken in JANUARY was 16.0 The average steps taken in FEBRUARY was 45.5 The average steps taken in MARCH was 75.0 The average steps taken in APRIL was 105.5 The average steps taken in MAY was 136.0 The average steps taken in JUNE was 166.5 The average steps taken in JULY was 197.0 The average steps taken in AUGUST was 228 .0 The average steps taken in SEPTEMBER was 258.5 The average steps taken in OCTOBER was 289.0 The average steps taken in NOVEMBER was 319.5 The average steps taken in DECEMBER was 350.0

The average steps taken in JANUARY was 16.0 The average steps taken in FEBRUARY was 45.5 The average steps taken in MARCH was 75.0 The average steps taken in APRIL was 105.5 The average steps taken in MAY was 136.0 The average steps taken in JUNE was 166.5 The average steps taken in JULY was 197.0 The average steps taken in AUGUST was 228 .0 The average steps taken in SEPTEMBER was 258.5 The average steps taken in OCTOBER was 289.0 The average steps taken in NOVEMBER was 319.5 The average steps taken in DECEMBER was 350.0

Add a comment
Know the answer?
Add Answer to:
The steps.txt file contains the number of steps a person taken each day for a year....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • An ordinal date consists of a year and a day within it, both of which are...

    An ordinal date consists of a year and a day within it, both of which are integers. The year can be any year in the Gregorian calendar while the day within the year ranges from one, which represents January 1, through to 365 (or 366 if the year is a leap year) which represents December 31. Ordinal dates are convenient when computing differences between dates that are a specific number of days (rather than months). For example, ordinal dates can...

  • are the steps taken each day and the number of calories consumed in a day a...

    are the steps taken each day and the number of calories consumed in a day a continuous or discrete variable?

  • 1. Write a C++ program that reads daily weather observation data from a file and writes...

    1. Write a C++ program that reads daily weather observation data from a file and writes monthly and annual summaries of the daily data to a file. a. The daily weather data will be contained in a file named wx_data.txt, with each line of the file representing the weather data for a single day. b. For each day, the date, precipitation amount, maximum temperature, and minimum temperature will be provided as tab-separated data with the following format: 20180101 0.02 37...

  • PLEASE WRITE CODE FOR C++ ! Time Validation, Leap Year and Money Growth PartA: Validate Day and Time Write a program tha...

    PLEASE WRITE CODE FOR C++ ! Time Validation, Leap Year and Money Growth PartA: Validate Day and Time Write a program that reads a values for hour and minute from the input test file timeData.txt . The data read for valid hour and valid minute entries. Assume you are working with a 24 hour time format. Your program should use functions called validateHour and validateMinutes. Below is a sample of the format to use for your output file  timeValidation.txt :   ...

  • Write a function to determine the day of the week a person was born given his...

    Write a function to determine the day of the week a person was born given his or her birthday. The following steps should be used to find the day of the week corresponding to any date from 1901 through the present. In the following explanation, the following terms will be helpful. Assuming I type in my birthday as "6 10 1981": The month is 6. The day of the month is 10. The year is 1981. Find the number of...

  • Use basic C++ 3. A text file, superstars.txt, contains statistics on cricket players. For each player,...

    Use basic C++ 3. A text file, superstars.txt, contains statistics on cricket players. For each player, the file contains the player's first name, last name, number of matches played, total number of runs scored, number of times the player scored 100 runs in a match, and the number of wickets taken. The last row of data in the file contains the word "END" only. Some rows are shown below 30 11867 164 Chanderpaul Shivnarine 34 11912 130 Lara Brian 73...

  • c++ please :) First, ask the user to enter a year (4-digit) and what week day...

    c++ please :) First, ask the user to enter a year (4-digit) and what week day does this year starts with (String) For example, the year 2018 starts with the week day "Friday". In this case, the calendar should start from January 1 which is Friday, 2018 Your program should produce a formatted calendar for the specified year and there should be a blank line after each month. Moreover, the month and the year should be centered over each month....

  • Hi any help is appreciated! I am using C++. The Julian Day Number (JDN) is a...

    Hi any help is appreciated! I am using C++. The Julian Day Number (JDN) is a sequential count of days since the beginning of the Julian Period. Day number zero corresponds to 1 January 4713 BCE. As a result of calendar reform in the 16th Century, we will only be computing day numbers since 15 October 1582 (which was the first day of the modern Gregorian calendar, having day number 2299161). 1 January 2001 was day number 2451911. To compute...

  • Write a C# program that prints a calendar for a given year. Call this program calendar....

    Write a C# program that prints a calendar for a given year. Call this program calendar. This program needs to use Switch Case in order to call the methods and format to print each month. The program prompts the user for two inputs:       1) The year for which you are generating the calendar.       2) The day of the week that January first is on, you will use the following notation to set the day of the week:      ...

  • In python Attached is a file called sequences.txt, it contains 3 sequences (one sequence per line)....

    In python Attached is a file called sequences.txt, it contains 3 sequences (one sequence per line). Also attached is a file called AccessionNumbers.txt. Write a program that reads in those files and produces 3 separate FATSA files. Each accession number in the AccessionNumbers.txt file corresponds to a sequence in the sequences.txt file. Remember a FASTA formatted sequence looks like this: >ABCD1234 ATGCTTTACGTCTACTGTCGTATGCTTTACGTCTACTGACTGTCGTATGCTTACGTCTACTGTCG The file name should match the accession numbers, so for 1st one it should be called ABCD1234.txt. Note:...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT