TypeError:' _io.TextIOWrapper'对象不可调用

时间:2016-04-04 03:36:50

标签: callable

我无法弄清楚如何修复我一直收到的错误,我将它们放在下面代码的底部。

 import random

此函数创建一个随机数文件

def random_number_file_create(min, max, filenames, entries):
    random_number_file  = open(FILENAME, "w")
    for i in range (entries):
            number = str(random.randint(min, max))
            random_number_file.write(number + "\n")
    random_number_file.close()

此函数打印随机数

def lines_print(filename):
    random_number_file = open(FILENAME, "r")
    line = random_number_file.readline()
    while line != "":
            print(line, end="")
            line = random_number_file()
    random_number_file.close()

此函数计算包含随机数

的行
def lines_count(filename):
    random_number_file = open (FILENAME, "r")
    line = random_number_file.readline()
    count = 0
    for number in random_number_file:
            count += 1
    return count
    random_number_file.close()

此函数添加数字

的总和
def total_numbers_in_file(filename):
    random_number_file = open (FILENAME, "r")
    line = random_number_file.readline()
    total=0
    for number in random_number_file:
            total = int(number)
    return total
    random_number_file.close()
FILENAME= "numbers.txt"
random.seed(83)
random_number_file_create(50,100,FILENAME,20)
lines_print(FILENAME)
print()
entries = lines_count(FILENAME)
total = total_numbers_in_file(FILENAME)
average = round(total/entries)
print("Entries:", entries)
print("Total:", total)
print("Average:", average)

#错误#     Traceback(最近一次调用最后一次):       文件" hw8.py",第41行,in         lines_print(文件名)       文件" hw8.py",第18行,在lines_print中         line = random_number_file()     TypeError:' _io.TextIOWrapper'对象不可调用

1 个答案:

答案 0 :(得分:-1)

提供参考案例:

另一个类似的案例,错误提示是:

Traceback(most recent call last): File "ex17.py", line 18, in <module> input<'?'> TypeError: 'file' object is not callable.

提问者支持他的代码,另一个技术丹尼尔发现了这一点 他使用&#39;输入&#39;作为一个变量,但在接下来的行中,他也使用了&#39;输入&#39;用于从用户获取数据的功能。这才是真正的原因。

当我运行我的模块时,系统会向您显示与您相同的错误,但我的错误与明显的提问者相同。因此,通过更改输入&#39;变量作为不同的变量&#39; input_1&#39;,我解决了错误。

我希望我的回答可以帮助你,实际上我是python的新手,我可以阅读你的代码并理解,但我无法找到真相。