从文本文件中查找最小值和最大值-Python

从文本文件中查找最小值和最大值-Python,python,Python,我需要从这个数据列表中找到最小值和最大值。我能得到最大值,但得到最小值。文本文件包含大量数据,因此我决定将其上传到此处: 输入 try: file_name = input("Enter the name of an input file ") input_file = open( file_name, "r" ) header=input_file.readline() count=0 total=0 largest_num=0 sm

我需要从这个数据列表中找到最小值和最大值。我能得到最大值,但得到最小值。文本文件包含大量数据,因此我决定将其上传到此处:

输入

try:
    file_name = input("Enter the name of an input file ")
    input_file = open( file_name, "r" )
    header=input_file.readline()
    count=0
    total=0  
    largest_num=0
    smallest_num=0
    michigan=""
    for line in input_file:
        output=float(line[67:71].rstrip())

        total += output
        count +=1
        if largest_num<= output:
            largest_num = output

        if smallest_num >= output:
            smallest_num = output

        if line[0:17].rstrip() == "Michigan":
            state=(line[0:17])
            number=(line[67:75])
    print("\nState with the smallest MMR vaccation rate:")
    print("   with a rate of")
    print("\n State with largest MMR vaccination rate:" )
    print("   with a rate of")

    print("\nThe calculated average vaccination rate is",round(total/count,1))
    print("")
    print("Michigan MMR vaccination rate is", number)
    input_file.close()
    except FileNotFoundError:
        print("Error: file not found")
        file_name = input("Enter the name of an input file ")
        input_file = open( file_name, "r" )
试试看:
文件名=输入(“输入输入文件名”)
输入文件=打开(文件名,“r”)
header=input_file.readline()
计数=0
总数=0
最大数量=0
最小数量=0
密歇根州=“”
对于输入_文件中的行:
输出=浮点(行[67:71].rstrip())
总+=产量
计数+=1
如果最大_num=输出:
最小数量=输出
如果行[0:17].rstrip()=“密歇根”:
状态=(第[0:17]行)
数字=(第[67:75]行)
打印(“\n以最小的MMR真空率声明:”)
打印(“速率为”)
打印(“\n最大MMR接种率的州:”)
打印(“速率为”)
打印(“\n计算的平均接种率为”,四舍五入(总数/计数,1))
打印(“”)
打印(“密歇根MMR疫苗接种率为”,编号)
输入_文件。关闭()
除FileNotFoundError外:
打印(“错误:找不到文件”)
文件名=输入(“输入输入文件名”)
输入文件=打开(文件名,“r”)

列表理解是你的朋友

numbers = [float(line[67:71].rstrip()) for line in input_file]

largest_num = max(numbers)
smallest_num = min(numbers)
total = sum(numbers)
count = len(numbers)

这与读取和/或写入文件有什么关系?实际问题是什么?请尝试修复缩进:)尝试查找MMR疫苗接种率的最小值和最大值(参见txt文件)您甚至从未打印过
最大值
最小值
;你怎么知道它们没有正确的值呢?你将
最小值
初始化为0,这样只有负值会改变它。你不需要去除浮点数,它会忽略空白