gpt4 book ai didi

python - 这个程序有什么问题吗?无法调试

转载 作者:行者123 更新时间:2023-11-30 23:04:53 24 4
gpt4 key购买 nike

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

# 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.")

我得到的错误是

Traceback (most recent call last):
File "C:/Python34/saves/midtermprep.py", line 42, in <module>
if min_res > result:
TypeError: unorderable types: int() > str()

我有这个程序的输入txt文档

NA
141
136
145
K
4.8
3.5
5.3
CL
100
98
107
CO2
20
22
32
CALCIUM
9.6
8.4

最佳答案

您将 int(min_res 或 max_res)与字符串(结果)进行比较,这不起作用。在比较之前将结果转换为 int

if min_res > int(result):
output_line = "low\n"
elif max_res < int(result):
output_line = "high\n"

关于python - 这个程序有什么问题吗?无法调试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33489903/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com