i have python (2.7) script reads input file contains text setup this:
steve 83 67 77
the script averages numbers corresponding each name , returns list each name, contains persons name along average, example return output looks this:
steve 75
however, actual average value "steve" "75.66666667". because of this, return value 76, not 75 (aka round nearest whole integer). i'm not sure how done... here code:
filename = raw_input('enter filename: ') file=open(filename,"r") line = file.readline() students=[] while line != "": splitedline=line.split(" ") average=0 in range(len(splitedline)-1) : average+=int(splitedline[i+1]) average=average/(len(splitedline)-1) students.append([splitedline[0],average]) line = file.readline() v in students: print " ".join(map(str, v)) file.close()
while code messy , should improved overall, solution problem should simple:
average=average/(len(splitedline)-1)
should be:
average /= float(len(splitedline) - 1) average = int(round(average))
by default in python 2.x /
2 integers flooring division. must explicitly make 1 of parameters floating point number real division. must round result , turn integer.
in python 3 flooring division //
, , regular division /
. can behavior in python 2 from __future__ import division
.