#Declare variables.
index = 0
monthName = ''
numDays = 0
monthTotal = 0
monthAverage = 0
try:
# Open inputFile with number of steps in it.
infile = open('steps.txt', 'r')
# Read contents of file into variable stepsPerDay.
stepsPerDay = infile.readlines()
# Create list by stripping off the newline characters.
for i in range(len(stepsPerDay)):
stepsPerDay[i] = stepsPerDay[i].rstrip()
print(stepsPerDay)
# Create a dictionary named calendar that has
# monthName as keys and the numDays (number of days in
# the month) as values.
calender = {'January':31}#, 'February':28, 'March':31, 'April':30, 'May':31, 'June':30, 'July':31, 'August':31, 'September':30, 'October':31, 'November':30, 'December':31}
# Loop through each month in year.
for monthName in calender:
monthTotal = XXXXX
monthAverage = XXXXXX
numDays = XXXXX
print(monthTotal)
print(monthName)
print(monthAverage)
I need to find the Month total, name, average, and the number of days a month. I can only change the XXXXX in the code because this is a fix-it exercise for school. I need to figure out the amount of steps walked each day, the total of those steps per month, and the average of each month. The amount of steps is stored in a.txt file and I don't know what code to use in order to only take the required steps from where they're indexed in the list. I'm new to python programming and my professor hasn't been a lot of help as of this assignment.
Supposing that the file steps.txt
contains a number of steps for every day in the year and that your variable index
is increased in your for
loop, the code may be
for monthName in calender:
monthTotal = sum(map(int, stepsPerDay[index: index + calender[monthName]]))
monthAverage = monthTotal / calender[monthName]
numDays = calender[monthName]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.