【发布时间】:2018-04-23 05:26:28
【问题描述】:
我是 Python 新手,我的代码有问题 我在输出中打印了错误的数据,如日期、年份、最低和最高温度。我不确定需要对代码进行哪些更改才能从文本文件中打印正确的最低和最高温度。谢谢您的帮助。
下面的代码输出如下所示:
日期:02/01
年份:2002
最低温度是:8
日期:02/06
年份: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))
main()
这是我的文件外观的示例。 (我只从文本文件中包含了我需要的列)
Day Max Year Min
---------------------------------------
---------------------------------------
02/01 79 2002 8
02/02 69 1989 5
02/03 83 1989 7
02/04 76 1957 3
02/05 98 1890 0
02/06 77 2008 9
【问题讨论】:
标签: python python-3.x