简体   繁体   English

你能看看我的代码吗,我不知道它有什么问题

[英]Could you look at my code, I don't know what's wrong with it

fname = input("Enter file name: ")
fh = open("mbox-short.txt")
inp = fh.read()
count = 0
for line in fh:
    line = line.rstrip()
    if not line.startswith("X-DSPAM-Confidence:") :
        continue
    count = count + 1
    c = float(count)
    print(len(inp))
    average = inp / c
print("Average spam confidence:", average)

The code above should write a program that prompts for a file name, then opens that file and reads through the file, looking for lines of the form:上面的代码应该编写一个程序,提示输入文件名,然后打开该文件并读取文件,查找以下形式的行:

X-DSPAM-Confidence: 0.8475 X-DSPAM-置信度:0.8475

These lines should be counted and the floating point values from each of the lines extracted to compute the average of those values and produce an output as shown below.应对这些行进行计数,并从每行中提取浮点值以计算这些值的平均值并生成如下所示的 output。 How could a possible code look like without the usage of the sum() function or a variable named sum in the solution?如果解决方案中没有使用 sum() function 或名为 sum 的变量,可能的代码会是什么样子? What is wrong with my code?我的代码有什么问题?

You are trying to iterate over an empty object.您正在尝试迭代一个空的 object。

The line inp = fh.read() has exhausted the fh object and it has become set to None inp = fh.read()行耗尽了 fh object 并且它已设置为 None

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM