【发布时间】:2021-11-09 04:23:59
【问题描述】:
我得到 TypeError: 'int' object is not callable on line 27 (var = ...)
文件data1_.txt只有5行,每行一个整数。
谁能帮我理解为什么?
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)
【问题讨论】:
-
我敢打赌,您之前将
sum重新定义为int。 How to Ask 和 minimal reproducible example. -
jfyi,python 在语句末尾不需要
; -
@Julien ,是的,我做到了,但我该如何解决呢?这是我第一次这样做。我读了其他答案,但我听不懂。
-
好吧,不要这样做。 (如果需要,重新启动您的 IPython 会话)
-
@Julien 所以你告诉我,如果我在 python 中重新定义了某些东西,它会更改为该实例中的所有编码?