Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 这个程序有什么问题?Can';t调试_Python_Debugging_Typeerror - Fatal编程技术网

Python 这个程序有什么问题?Can';t调试

Python 这个程序有什么问题?Can';t调试,python,debugging,typeerror,Python,Debugging,Typeerror,我花了几个小时试图调试这个程序,但我还没有弄明白为什么它不能正常工作。这个程序应该读取测试结果,在控制台从用户处获取结果文件,将摘要行写入输出文件,并在任何结果超出正常范围时向控制台打印标志 # str file_name, test_name, output_line, result_str, min_res_str, max_res_str # file results_file, output_file # int result, min_res, max_res # bool done

我花了几个小时试图调试这个程序,但我还没有弄明白为什么它不能正常工作。这个程序应该读取测试结果,在控制台从用户处获取结果文件,将摘要行写入输出文件,并在任何结果超出正常范围时向控制台打印标志

# str file_name, test_name, output_line, result_str, min_res_str, max_res_str
# file results_file, output_file
# int result, min_res, max_res
# bool done


done = False
flag = True

file_name = input("Please enter the name of the file, without the .txt   extension: ")
results_file = open(file_name+".txt")
output_file = open(file_name+"_output.txt", "w")

while not done:
# read a test result
test_name = results_file.readline()
test_name == test_name.rstrip("\n")
if test_name != "":
    result_str = results_file.readline()
    if result_str != "":
        result = str(result_str)
        min_res_str = results_file.readline()
        if min_res_str != "":
            min_res = int(min_res_str)
            max_res_str = results_file.readline()
            max_res = int(max_res_str)
        else:
            done = True
    else:
        done = True
else:
    done = True
# process
if not done:
    output_line = output_file.write(test_name+"\t" + str(result)+"\t")
    if min_res > result:
        output_line = "low\n"
    elif max_res < result:
        output_line = "high\n"
    else:
        output_line = "within normal limits\n"
        flag = False
    output_file.write(output_line)

results_file.close()
output_file.close()

if flag:
     print("Test result out of range: check results.")
您将int(最小分辨率或最大分辨率)与字符串(结果)进行比较,但结果不起作用。比较前将结果转换为整数

if min_res > int(result):
    output_line = "low\n"
elif max_res < int(result):
    output_line = "high\n"
如果最小值>整数(结果):
输出\u line=“低\n”
elif max_res
Result是一个字符串,min\u res是int。无法将
Result=str(Result\u str)
您的施法
Result
字符串进行比较。尝试将
if min\u res>result:
更改为
if min\u res>int(result)
:经过进一步研究,将
if min\u res>result:
更改为
if min\u res>float(result):
是否更有意义,因为数字不仅是整数,而且也是浮点数?我已经测试过了,但是收到了另一个错误<代码>回溯(最近一次调用):文件“C:/Python34/saves/midterprep.py”,第32行,在max\u res=float(max\u res\u str)ValueError:无法将字符串转换为float:
@ptierno我已更改了这些,因此错误更改了“回溯”(最近一次调用):文件“C:/Python34/saves/midterprep.py”,第30行,在min_res=int(min_res_str)ValueError中:以10为基数的int()的文本无效:“3.5\n”查看示例文件,K和cacium的值实际上是浮点数,而不是int。现在,每次在程序中将字符串转换为int(aka
int(…)
),都应该将其转换为float(aka
float(…)
NA
141
136
145
K
4.8
3.5
5.3
CL
100
98
107
CO2
20
22
32
CALCIUM
9.6
8.4
if min_res > int(result):
    output_line = "low\n"
elif max_res < int(result):
    output_line = "high\n"