Search code examples
pythonvariant

Calculate variant of the numbers / TypeError: 'int' object is not callable


I get the TypeError: 'int' object is not callable on the line 27 (var = ...)

the file data1_.txt is just 5 lines with a integer number in each line.

Can someone help me to understand why?

line_number = 0;
countline = 0;
linesum = 0;
avg = 0;
var = 0;
linelist = [];

f = open("data1_.txt", "r")

while True:
#how to count lines    
    line = f.readline()
    if line == "" :
        break
    line = line.rstrip("\n\r")
    line_number += 1
    if line != "/n":
        countline += 1
#how to list the numbers     
    x = line.strip()
    linelist.append(x)
#how to sum the numbers of the list
    linesum += int(x)*1.0
    avg = linesum/countline;

#how to calculate variant of numbers
    var = sum((y-avg) **2 for y in linelist) / countline
    
f.close()

print("number of lines:", countline)
print("the numbers:", linelist)
print("sum of numbers:", linesum)
print("avarage:", avg)
print("variant:", var)

Solution

  • I found your problem. in your sum statement you are trying to compare the string value of y (which is what readlines gives) when it should be a float value.

    Try this:

    #how to calculate variant of numbers
        var = sum((float(y)-avg) **2 for y in linelist) / countline