In: Computer Science
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() |
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...!!!