2 回答
TA贡献1797条经验 获得超4个赞
在这里,我已经修复了您的代码的一些逻辑错误,请查找注释以查看位置,
def main():
rainfall_inch = []
name_month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
for item in name_month:
print("Enter rainfall" + item, ": ", end=' ')
x = input()
if x == '':
x = 0
rainfall_inch.append(x) # you are appending here
b = [float(i) for i in rainfall_inch] # so remove the range here, its wrong
# range generates integers from a to b where a and b are integers
print(b)
print("average rainfall is:", computeAverage(b))
def computeAverage(values):
#find average of rainfull
total = 0.0
for x in values:
total+=x
average = total/len(values)
return average
main()
输出:
Enter rainfallJanuary : 40
Enter rainfallFebruary : 50
Enter rainfallMarch : 30
Enter rainfallApril : 60
Enter rainfallMay : 50
Enter rainfallJune : 30
Enter rainfallJuly : 330
Enter rainfallAugust : 20
Enter rainfallSeptember : 50
Enter rainfallOctober : 90
Enter rainfallNovember : 30
Enter rainfallDecember : 430
[40.0, 50.0, 30.0, 60.0, 50.0, 30.0, 330.0, 20.0, 50.0, 90.0, 30.0, 430.0]
average rainfall is: 100.83333333333333
添加回答
举报