Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.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
Python3-编写一个从文件中读取数字的程序,月份名称无法打印,错误_Python_Python 3.x - Fatal编程技术网

Python3-编写一个从文件中读取数字的程序,月份名称无法打印,错误

Python3-编写一个从文件中读取数字的程序,月份名称无法打印,错误,python,python-3.x,Python,Python 3.x,我需要帮助使我的代码正常工作。由于某些原因,它不会显示月份名称,并且在计算总降雨量时给了我一个错误 第58行,总降雨量 回报总额(降雨量) TypeError:“float”对象不可编辑 这是我的作业 编写一个程序,将12个月的总降雨量读取到一个列表中。该程序应计算并显示该年的总降雨量、月平均降雨量以及降雨量最高和最低的月份 def monthsList(): #creates a list of the months monthList = ['January', 'Fe

我需要帮助使我的代码正常工作。由于某些原因,它不会显示月份名称,并且在计算总降雨量时给了我一个错误

第58行,总降雨量 回报总额(降雨量) TypeError:“float”对象不可编辑

这是我的作业 编写一个程序,将12个月的总降雨量读取到一个列表中。该程序应计算并显示该年的总降雨量、月平均降雨量以及降雨量最高和最低的月份

def monthsList():
       #creates a list of the months
    monthList = ['January', 'Febuary', 'March','April','May', 'June','July', 'August','September','October', 'November', 'December']
    return monthList



def totalRainfall(rainfall):
#gets sum of all rainfall
    return sum(rainfall)



def averageRainfall(rainfall):
#gets the sum of rainfall and divides by 12 to get the average
    return sum(rainfall)/12



def maxRainfall(monthList):
#finds the max index number in monthList
# adds one so it wont start at 0
    return monthList.index(max(monthList))+1



def minRainfall(monthsList):
#finds the min index number in monthList
# adds one so it wont start at 0
    return monthList.index(min(monthList))+1

def main():


inFile = open('program9.txt', 'r')

rainfall = [] # set rainfall to an empty list

lineRead = inFile.readline()       # Read first record
while lineRead != '':              # While there are more records
   words = lineRead.split()        # Split the records into substrings
   rainfall = float(words[0])
   print(format(rainfall, '.2f'))

   lineRead = inFile.readline()    # Read next record

totR=totalRainfall(rainfall) #assigns totR to the rainfall total
avgR=averageRainfall(rainfall) # assigns avgR to rainfall average
maxR=maxRainfall(rainfall) # assigns maxR to the maxrainfall
minR=minRainfall(rainfall) #assigns minR to minrainfall 

print('The total Rainfall for the year was: ',format(totR, ',.2f'), sep='')
print('The average Rainfall for the year was: ', format(avgR, ',.2f'), sep='')
print('The month with highest rainfall was:',maxR)
print('The month with lowest rainfall was:',minR)

inFile.close() # Close file
# End of the main function 

main()
文本文件为:

1.89

1.99

2.14

2.51

5.03

3.81

1.97

2.31

2.91

3.97

2.68


2.44

您可以尝试将“Rainsion=float(words[0])”替换为“Rainsion.append(float(words[0])”

您最初有
Rainsion=[]
,然后您做了
Rainsion=float(words[0])
,它将降雨从列表重新定义为float。因此,
TotalRainsion
得到了一个浮点数,而不是一个列表。我建议学习如何使用调试器逐行检查代码。然后,您可以在代码中检查每个变量的类型和值。几乎所有这些函数都是多余的,它们只会使程序更加复杂。此外,变量和函数名称通常应遵循带有下划线的
小写形式。谢谢你的提示,你有调试器建议吗?我一定会去修复名称的,谢谢。我不确定如何修复这些功能。你能告诉我你所说的多余是什么意思吗?