Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 采取的平均步骤_Python - Fatal编程技术网

Python 采取的平均步骤

Python 采取的平均步骤,python,Python,我的程序运行,但只输出“月平均值”。我需要它张贴多少步骤是在每个月采取 days = [31,28,31,30,31,30,31,31,30,31,30,31] months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] firstIndex = 0 lastIndex = 0 inputFi

我的程序运行,但只输出“月平均值”。我需要它张贴多少步骤是在每个月采取

days = [31,28,31,30,31,30,31,31,30,31,30,31]
months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
firstIndex = 0
lastIndex = 0

inputFile = open(r'C\Users\UserName\Downloads\steps.txt', 'r')
outputFile = open(r'C\Users\UserName\Downloads\stepsbymonth.txt', 'w')

outputFile.write('MONTH | AVERAGE\n')
print('MONTH | AVERAGE\n')
lines = inputFile.readlines()
lines = list(map(int,lines))

for x in range(0, 12):
    lastIndex = firstIndex + days[x]
    monthLines = lines[firstIndex:lastIndex]
    average = float(sum(monthLines)) / max(len(monthLines),1)
    outputFile.write(months[x] + '|' + '{0:.1f}'.format(average)+ '\n')
    firstIndex = lastIndex

inputFile.close()
outputFile.close()

您没有打印号码

另外,请使用
outputFile=open(r'C\Users\UserName\Downloads\stepsbymonth.txt',a'
,因为您不想覆盖该文件。

我不知道这是否会被视为一种改进。我在学校只学了三周python,但这就是我处理作业的方式
这个Python不是很惯用。您可能希望将上下文管理器作为文件I/O的起点。此外,您没有提出任何问题。您已经计算了这个数字,只需将其写出
sum(monthline)
当我添加sum(monthline)时,它说monthline没有定义
# Global Variables/Constants
month = ('January', 'February', 'March', 'April', 'May', 'June', 'July',
         'August', 'September', 'October', 'November', 'December')
days = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
# Main Module
def main():
    # This calls the welcome module
    welcome()
    # This calls the Step Tracker module
    stepTracker()
# Welcome Module
def welcome():
    print ('Here is your workout information for the year:')
    print ('__________________________________________________________________')
    print ('')
# Counter Module
def stepTracker():
    # This opens the steps.txt file
    stepCounter = open('steps.txt', 'r')
    # This sets the month count to zero which begins the tuple.
    monthCount = 0
    # This loops through the months until the 12th month is reached
    for num in range(1, 13):
        # This resets the base values for the counters on each months iteration
        totalSteps = 0
        count = 0
        average = 0
        # This iterates until the end of the month is reached
        for count in range(1, days[monthCount] + 1):
            # This reads the lines until the end of the month is reached
            steps = int(stepCounter.readline())
            # This sums the months
            totalSteps = totalSteps + steps
        # This averages the steps for the month
        average = totalSteps / days[monthCount]
        # This prints the average for the month
        print ('The average steps taken for the month of ' + month[monthCount] +
               ' is: ' + format(average, ',.1f') + ' steps.')
        # This increments the months count
        monthCount = monthCount + 1
# This calls the main module
main()