Question

In: Computer Science

Using steps.py in this repository for a starter file, write a Python program to calculate average...

Using steps.py in this repository for a starter file, write a Python program to calculate average number of steps per month for a year.

The input file (which you will read from the command line, see the sample program on how to read command line arguments in this repository) contains the number of steps (integer) a person took each day for 1 year, starting January 1. Each line of input contains a single number.

Assume this is NOT a leap year.

Your output must follow this format:

The average steps taken in MONTH NAME was 9999.9

Where 9999.9 represents that month's average. Be sure to output only a single decimal digit (digit to the right of the decimal)

Be sure to spell out the name of the month. First letter must be caps. For example: June

For a grade of A your program should handle a leap year by looking at the number of steps in the file, if 365 then non leap year, if 366 then leap year.

Submit your source code to CodePost.io

All programs you submit should have the following comments at the start of your source code file.

# Your Name
# CSCI 236
# Date
# Program 01 - Steps
# approximate number of hours you invested
# Grade Version (so if you did the A version say: A version)
# description of any major problems you ran into
# status of the program - does it compile, does it run perfectly, does it have bugs, any limitations, etc

steps.py

# Named constants - create one for each month to store number of days in that month; assume this is NOT a leap year
def main():
# Open the steps file using the first command line argument to get the input file name. For example: python steps.py steps.txt would open the file steps.txt to read the steps
# Display the average steps for each month using a function to calculate and display
# Close the file.
def average_steps(steps_file, month_name, days):
# compute the average number of steps for the given month
# output the results
main()

Solutions

Expert Solution

Code:-

import sys
def average_steps(steps):
   #(month, days) List of tuples
   #Feb has 28 days
   NonLeap_months = [("January", 31), ("February", 28), ("March", 31), ("April", 30),
   ("May", 31), ("June", 30), ("July", 31), ("August", 31),
   ("September", 30), ("October", 31), ("November", 30), ("December", 31)]
   #feb has 29 days
   Leap_months = [("January", 31), ("February", 29), ("March", 31), ("April", 30),
   ("May", 31), ("June", 30), ("July", 31), ("August", 31),
   ("September", 30), ("October", 31), ("November", 30), ("December", 31)]
   months = []
   if len(steps) == 365:   #according to number of days set months
       months = NonLeap_months
   else:
       months = Leap_months
   start = 0           #start at index start
   for month in months:   #for every month in list
       totalSteps = 0       #count totalSteps
       i = start           #from index start to start + number of days in that month
       while i < (start + month[1]):
           totalSteps = totalSteps + steps[i]   #add steps to total
           i = i + 1
       avgSteps = totalSteps/month[1]   #compute average
       print("The average steps taken in " + month[0] + " was " + str(round(avgSteps, 1)))
       start = start + month[1]   #update start for next month
def main():
   if len(sys.argv)!=2: #check if command line argument is given
       print("Enter python step.py filename")
       sys.exit()
   steps = []
   myfile = open(sys.argv[1], 'r')           #read input file
   for line in myfile:                       #read each line
       line = line.strip("\n")               #strip newline char from line
       if line != "":
           steps.append(int(line))           #append integer to steps list
   myfile.close()                           #close the file
   average_steps(steps)                   #compute and print average
main()

Code Screenshot:-

Input File:-

Output:-

Please UPVOTE thank you...!!!


Related Solutions

Python: Write a program that asks the user for the name of a file. The program...
Python: Write a program that asks the user for the name of a file. The program should display the contents of the file line by line.
Write a python program: There is a file called file 2. File2 is a txt file...
Write a python program: There is a file called file 2. File2 is a txt file and I have written the contents of file 2 below in the exact format it was in notepad. # This comment does not make sense # It is just to make it harder # The job description starts after this comment, notice that it has 4 lines. # This job description has 700150 hay system points\\ the incumbent will administer the spending of kindergarden...
Write a program using python that loops over each file in a specified directory and checks...
Write a program using python that loops over each file in a specified directory and checks the size of each file.You should create 2-tuple with the filename and size, should append the 2-tuple to a list, and then store all the lists in a dictionary.  
Get the file “HW4Part4b.java” from the repository. Modify the program class to match the new file...
Get the file “HW4Part4b.java” from the repository. Modify the program class to match the new file name. Complete it by writing a recursive static int function named recur that is defined as follows: if i ≤ 0 or j ≤ 0, recur(i, j) = 0. if i = j, recur(i, j) = i. if i > j, recur(i, j) = j. In all other cases, recur(i, j) = 2 · recur(i − 1, j) + recur(j − 1, i) Add...
[In Python] Write a program that takes a .txt file as input. This .txt file contains...
[In Python] Write a program that takes a .txt file as input. This .txt file contains 10,000 points (i.e 10,000 lines) with three co-ordinates (x,y,z) each. From this input, use relevant libraries and compute the convex hull. Now, using all the points of the newly constructed convex hull, find the 50 points that are furthest away from each other, hence giving us an evenly distributed set of points.
Need this program Using Classes , Objects, Save to file, and Printbill Simple python assignment Write...
Need this program Using Classes , Objects, Save to file, and Printbill Simple python assignment Write a menu-driven program for Food Court. (You need to use functions!) Display the food menu to a user (Just show the 5 options' names and prices - No need to show the Combos or the details!) Ask the user what he/she wants and how many of it. (Check the user inputs) AND Use strip() function to strip your inputs. Keep asking the user until...
Write a program in python to read from a file the names and grades of a...
Write a program in python to read from a file the names and grades of a class of students to calculate the class average, the maximum, and the minimum grades. The program should then write the names and grades on a new file identifying the students who passed and the students who failed. The program should consist of the following functions: a) Develop a getGrades() function that reads data from a file and stores it and returns it as a...
Write a Python program that has the user enter their name.   Using the user's name calculate...
Write a Python program that has the user enter their name.   Using the user's name calculate the following: 1. How many letters are in the user's name? 2. Print the user's name in REVERSE both in capital letters and lowercase letters 3. Print the ASCII value for each letter of the user's name. 4. Print the SUM of all of the letters of the user's name (add each letter from #3)
Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
Step by step in python please Write a program this will read a file (prompt for...
Step by step in python please Write a program this will read a file (prompt for name) containing a series of numbers (one number per line), where each number represents the radii of different circles. Have your program output a file (prompt for name) containing a table listing: the number of the circle (the order in the file) the radius of the circle the circumference the area of the circle the diameter of the circle Use different functions to calculate...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT