Please trim unrelated text from emails. On 21 March 2013 10:42, Arijit Ukil <arijit.u...@tcs.com> wrote:
> I am new to python. I like to calculate average of the numbers by reading > the file 'digi_2.txt'. I have written the following code: > > def average(s): return sum(s) * 1.0 / len(s) > > f = open ("digi_2.txt", "r+") > > list_of_lists1 = f.readlines() > > > for index in range(len(list_of_lists1)): > > > tt = list_of_lists1[index] > > print 'Current value :', tt > > avg =average (tt) > > > This gives an error: > > def average(s): return sum(s) * 1.0 / len(s) > TypeError: unsupported operand type(s) for +: 'int' and 'str' > tt is a string as it's read from the file. int(tt) would fix the problem. But in addition you're also not actually calculating the average. def average(s): return sum(s) / len(s) # convert the list of strings to a list of floats tt = [float(x) for x in list_of_lists1] avg = average(tt) -- ./Sven
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor