Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/297.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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 我需要哪行代码来查找monthTotal、monthAverage和numDays?_Python_List_Dictionary - Fatal编程技术网

Python 我需要哪行代码来查找monthTotal、monthAverage和numDays?

Python 我需要哪行代码来查找monthTotal、monthAverage和numDays?,python,list,dictionary,Python,List,Dictionary,我需要找到一个月的总数、名称、平均数和一个月的天数。我只能更改代码中的XXXXX,因为这是学校的修复练习。 我需要计算出每天走的步数,每个月走的步数,以及每个月的平均数。步骤的数量存储在一个.txt文件中,我不知道使用什么代码,以便只从列表中的索引位置执行所需的步骤。我是python编程新手,我的教授在这项作业中没有得到太多帮助。假设文件steps.txt包含一年中每天的许多步骤,并且变量索引在for循环中增加,代码可能是 #Declare variables. index = 0 monthN

我需要找到一个月的总数、名称、平均数和一个月的天数。我只能更改代码中的XXXXX,因为这是学校的修复练习。
我需要计算出每天走的步数,每个月走的步数,以及每个月的平均数。步骤的数量存储在一个.txt文件中,我不知道使用什么代码,以便只从列表中的索引位置执行所需的步骤。我是python编程新手,我的教授在这项作业中没有得到太多帮助。

假设文件
steps.txt
包含一年中每天的许多步骤,并且变量
索引
for
循环中增加,代码可能是

#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)

我不知道那个文件里有什么,但这里有一个可能的解决方案:
monthTotal=int(stepsPerDay[index])*calendar[monthName]
monthAverage=monthTotal/12
numDays=calendar[monthName]
对于家庭作业问题,你真的应该在提问之前展示你自己的工作。
for monthName in calender:
    monthTotal = sum(map(int, stepsPerDay[index: index + calender[monthName]]))
    monthAverage = monthTotal / calender[monthName]
    numDays = calender[monthName]