0

This program asks for the name of a file with weather data, like rettet_verdata_florida.txt. It then opens the file, and asks for the name of the file you want the specific data to be written into. It also asks for the year.

It is then supposed to read in the data for the different months of that year(which is separated by a \t in the text file), put the sums for the months in "sum_temp" and "sum_wind", and calculate the average while printing it out to both the file and the screen. But the average ends up being 0.0 for each month, even though it should work fine.

Here is some of the list:

Stnr    Dato    DD06    DD12    DD18    FFM FXM POM TAM UUM
50540   07.01.1957  150 170 170 6.2 8.8 1010.6  6.3 94
50540   08.01.1957  160 160 200 7.2 9.8 1001.8  8.0 99
50540   09.01.1957  290 200 160 8.1 13.3    990.2   5.7 91
50540   10.01.1957  300 360 350 12.7    15.4    1008.2  2.8 87
50540   11.01.1957  0   200 160 3.8 10.4    1015.1  1.7 98
50540   12.01.1957  0   330 340 5.1 10.4    995.0   5.0 96
50540   13.01.1957  350 130 60  6.5 12.3    1018.2  1.6 54
50540   14.01.1957  0   130 150 3.1 7.0 1033.0  -2.8    69

Here is the code:

def year_avg():

    #Asks for the file name to be opened, file name for storage, and year (buffer against error).
    file_opening = input("Write the file to be opened (with .txt): ") or "rettet_verdata_florida.txt"
    file_open = open(file_opening, "r")
    writing_file = input("Which file do you want it written to, sir?: ") or "file_for_data.txt"
    year = float(input("And which year do you want it from?: "))

    #Constants and sums
    number_of_days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
    sum_temp = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    sum_wind = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

    #For leap years
    if year % 4 == 0:
        number_of_days[2] = 29


    #In case the teacher tries anything funny
    if (writing_file == "rettet_verdata_florida.txt" or "verdata_kirkenes.txt" or "verdata_florida.txt"):
        writing_file = "file_for data.txt"


    #Makes the writing file, and picks the data for that year
    store_data = open(writing_file, "w")

    for line in file_open:
        try:
            split_numbers = line.split('\t')
            date_info = split_numbers[1].split('.')
            year_of_line = float(date_info[2])
            month_of_line = float(date_info[1])

            if year_of_line == year:
                sum_temp[int(month_of_line)] += float(split_numbers[8])
                sum_wind[int(month_of_line)] += float(split_numbers[5])

        except Exception:
            pass


    #Prints everything out, plus stores it in the file specified in "writing_file"
    year_in_comma = (float(year))/10000
    print("Dato\tTAM\tFFM")

    for i in range(1, 13):
        print(i + year_in_comma, sum_temp[i] / number_of_days[i] , sum_wind[i] / number_of_days[i], sep = "\t", file = store_data) 
        print(i + year_in_comma, sum_temp[i] / number_of_days[i] , sum_wind[i] / number_of_days[i], sep = "\t")


    store_data.close()
    file_open.close()
4

1 回答 1

0

这里的问题之一是:你有:

year = float(input("And which year do you want it from?: "))

并且year是一个float。然后你有:

date_info = split_numbers[1].split('.')
year_of_line = date_info[2]
month_of_line = date_info[1]

if year_of_line == year: 

哪里year_of_linestr,那是条件总是假的。因此,您的数据始终为零。如果您一直在使用python 2.x,这个问题可能更容易检测到,因为您的结果将全部是0而不是0.

于 2013-09-13T01:59:05.563 回答