Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.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 3_Python_Python 3.x - Fatal编程技术网

文本文件中的最低值和最高值-python 3

文本文件中的最低值和最高值-python 3,python,python-3.x,Python,Python 3.x,我是Python新手,我的代码有问题,我的输出中打印了错误的数据,如日期、年份、最低温度和最高温度。我不确定需要在代码中做什么更改才能从文本文件中打印正确的最低和最高温度。谢谢你的帮助 def main(): file = open ('open_File.txt', 'r') lowest = 200 lowest_day = "" lowest_year = "" highest = 0 highest_day = "" highest_year = "" for line in

我是Python新手,我的代码有问题,我的输出中打印了错误的数据,如日期、年份、最低温度和最高温度。我不确定需要在代码中做什么更改才能从文本文件中打印正确的最低和最高温度。谢谢你的帮助

def main():


file = open ('open_File.txt', 'r')

lowest = 200
lowest_day = ""
lowest_year = ""
highest = 0
highest_day = ""
highest_year = ""



for line in file.read().splitlines():
        if line[0].isdigit():
            values = line.strip().split()
            low = (values[3])
            high = (values[1])

            for lowest in low:
                if low < lowest:
                    lowest = low
                    lowest_day = values[0]
                    lowest_year = values[2]



            for highest in high:
                if high > highest:
                    highest = high
                    highest_day = values[0]
                    highest_year = values[2]




print ("Day: ", (lowest_day))
print ("Year: ", (lowest_year))
print ("The lowest temperature is: ", (lowest))


print ("Day: ", (highest_day))
print ("Year: ", (highest_year))
print ("The highest temperature is: ", (highest))
下面是我对以下代码的输出:

def main():


file = open ('open_File.txt', 'r')

lowest = 200
lowest_day = ""
lowest_year = ""
highest = 0
highest_day = ""
highest_year = ""



for line in file.read().splitlines():
        if line[0].isdigit():
            values = line.strip().split()
            low = (values[3])
            high = (values[1])

            for lowest in low:
                if low < lowest:
                    lowest = low
                    lowest_day = values[0]
                    lowest_year = values[2]



            for highest in high:
                if high > highest:
                    highest = high
                    highest_day = values[0]
                    highest_year = values[2]




print ("Day: ", (lowest_day))
print ("Year: ", (lowest_year))
print ("The lowest temperature is: ", (lowest))


print ("Day: ", (highest_day))
print ("Year: ", (highest_year))
print ("The highest temperature is: ", (highest))
日期:2001年2月
年份:2002年
最低温度为:8
日期:2006年2月
年份:2008年
最高温度是:77

def main():


file = open ('open_File.txt', 'r')

lowest = 200
lowest_day = ""
lowest_year = ""
highest = 0
highest_day = ""
highest_year = ""



for line in file.read().splitlines():
        if line[0].isdigit():
            values = line.strip().split()
            low = (values[3])
            high = (values[1])

            for lowest in low:
                if low < lowest:
                    lowest = low
                    lowest_day = values[0]
                    lowest_year = values[2]



            for highest in high:
                if high > highest:
                    highest = high
                    highest_day = values[0]
                    highest_year = values[2]




print ("Day: ", (lowest_day))
print ("Year: ", (lowest_year))
print ("The lowest temperature is: ", (lowest))


print ("Day: ", (highest_day))
print ("Year: ", (highest_year))
print ("The highest temperature is: ", (highest))
给你

def main():


file = open ('open_File.txt', 'r')

lowest = 200
lowest_day = ""
lowest_year = ""
highest = 0
highest_day = ""
highest_year = ""



for line in file.read().splitlines():
        if line[0].isdigit():
            values = line.strip().split()
            low = (values[3])
            high = (values[1])

            for lowest in low:
                if low < lowest:
                    lowest = low
                    lowest_day = values[0]
                    lowest_year = values[2]



            for highest in high:
                if high > highest:
                    highest = high
                    highest_day = values[0]
                    highest_year = values[2]




print ("Day: ", (lowest_day))
print ("Year: ", (lowest_year))
print ("The lowest temperature is: ", (lowest))


print ("Day: ", (highest_day))
print ("Year: ", (highest_year))
print ("The highest temperature is: ", (highest))
file = open('text.txt', 'r')

lowest = 200
lowest_day = 0
lowest_year = 0
highest = 0
highest_day = 0
highest_year = 0


for line in file.read().splitlines():
    if line[0].isdigit():
        values = line.strip().split()
        low = int(values[3])
        high = int(values[1])

        if low < lowest:
            lowest = low
            lowest_day = values[0]
            lowest_year = values[2]

        if high > highest:
            highest = high
            highest_day = values[0]
            highest_year = values[2]

print ("Day: ", (lowest_day))
print ("Year: ", (lowest_year))
print ("The lowest temperature is: ", (lowest))


print ("Day: ", (highest_day))
print ("Year: ", (highest_year))
print ("The highest temperature is: ", (highest))

一些注意事项:
对于low中的lower:
没有意义,因为
low
只是一个字符,而不是列表或其他什么,所以我删除了它。另外,您将整数与字符串进行比较,因此我将字符串转换为整数。

您的错误是因为有两个变量名为lower(和两个变量名为highest)。创建循环时

def main():


file = open ('open_File.txt', 'r')

lowest = 200
lowest_day = ""
lowest_year = ""
highest = 0
highest_day = ""
highest_year = ""



for line in file.read().splitlines():
        if line[0].isdigit():
            values = line.strip().split()
            low = (values[3])
            high = (values[1])

            for lowest in low:
                if low < lowest:
                    lowest = low
                    lowest_day = values[0]
                    lowest_year = values[2]



            for highest in high:
                if high > highest:
                    highest = high
                    highest_day = values[0]
                    highest_year = values[2]




print ("Day: ", (lowest_day))
print ("Year: ", (lowest_year))
print ("The lowest temperature is: ", (lowest))


print ("Day: ", (highest_day))
print ("Year: ", (highest_year))
print ("The highest temperature is: ", (highest))
for lowest in low:
Python遍历“low”中的字符,并比较它们是否小于“low”(这毫无意义)。变量“lowest”已被该for循环覆盖,因此程序会给出错误的结果。此外,为了找到最低温度,与最低值进行比较的数据必须是数字数据类型,而不是字符串类型

def main():


file = open ('open_File.txt', 'r')

lowest = 200
lowest_day = ""
lowest_year = ""
highest = 0
highest_day = ""
highest_year = ""



for line in file.read().splitlines():
        if line[0].isdigit():
            values = line.strip().split()
            low = (values[3])
            high = (values[1])

            for lowest in low:
                if low < lowest:
                    lowest = low
                    lowest_day = values[0]
                    lowest_year = values[2]



            for highest in high:
                if high > highest:
                    highest = high
                    highest_day = values[0]
                    highest_year = values[2]




print ("Day: ", (lowest_day))
print ("Year: ", (lowest_year))
print ("The lowest temperature is: ", (lowest))


print ("Day: ", (highest_day))
print ("Year: ", (highest_year))
print ("The highest temperature is: ", (highest))
要解决这个问题,您应该去掉“for low in low line”,只需为每天的最低温度创建一个变量(例如lineLow)。此变量将等于

def main():


file = open ('open_File.txt', 'r')

lowest = 200
lowest_day = ""
lowest_year = ""
highest = 0
highest_day = ""
highest_year = ""



for line in file.read().splitlines():
        if line[0].isdigit():
            values = line.strip().split()
            low = (values[3])
            high = (values[1])

            for lowest in low:
                if low < lowest:
                    lowest = low
                    lowest_day = values[0]
                    lowest_year = values[2]



            for highest in high:
                if high > highest:
                    highest = high
                    highest_day = values[0]
                    highest_year = values[2]




print ("Day: ", (lowest_day))
print ("Year: ", (lowest_year))
print ("The lowest temperature is: ", (lowest))


print ("Day: ", (highest_day))
print ("Year: ", (highest_year))
print ("The highest temperature is: ", (highest))
int(values[3])
这同样适用于更高的温度

def main():


file = open ('open_File.txt', 'r')

lowest = 200
lowest_day = ""
lowest_year = ""
highest = 0
highest_day = ""
highest_year = ""



for line in file.read().splitlines():
        if line[0].isdigit():
            values = line.strip().split()
            low = (values[3])
            high = (values[1])

            for lowest in low:
                if low < lowest:
                    lowest = low
                    lowest_day = values[0]
                    lowest_year = values[2]



            for highest in high:
                if high > highest:
                    highest = high
                    highest_day = values[0]
                    highest_year = values[2]




print ("Day: ", (lowest_day))
print ("Year: ", (lowest_year))
print ("The lowest temperature is: ", (lowest))


print ("Day: ", (highest_day))
print ("Year: ", (highest_year))
print ("The highest temperature is: ", (highest))

(另外,您可以使用file.read().splitlines()来代替file.readlines())

如果您想要一种更具python风格的方式,那么我认为下面的代码可能也会有所帮助

def main():


file = open ('open_File.txt', 'r')

lowest = 200
lowest_day = ""
lowest_year = ""
highest = 0
highest_day = ""
highest_year = ""



for line in file.read().splitlines():
        if line[0].isdigit():
            values = line.strip().split()
            low = (values[3])
            high = (values[1])

            for lowest in low:
                if low < lowest:
                    lowest = low
                    lowest_day = values[0]
                    lowest_year = values[2]



            for highest in high:
                if high > highest:
                    highest = high
                    highest_day = values[0]
                    highest_year = values[2]




print ("Day: ", (lowest_day))
print ("Year: ", (lowest_year))
print ("The lowest temperature is: ", (lowest))


print ("Day: ", (highest_day))
print ("Year: ", (highest_year))
print ("The highest temperature is: ", (highest))
file = open ('temp.txt', 'r')

myfunc1= lambda s: s[3]
myfunc2= lambda s: s[1]

lines = [line.strip().split() for line in file.read().splitlines() if line[0].isdigit()]

lowest= sorted(lines, key=myfunc1)[0]
highest = sorted(lines, key=myfunc2, reverse=True)[0]

print("lowestDAY: {0}, lowestYear: {1}, lowestTemp: {2}".format(lowest[0], lowest[2], lowest[3]))
print("highestDAY: {0}, highestYear: {1}, highestTemp: {2}".format(highest[0], highest[2], highest[1]))

当然,如果它对您有效,请标记为正确:)