После прочтения внешнего файла и присвоения строк переменной, как мне использовать эту переменную в другой функции? - PullRequest
0 голосов
/ 19 апреля 2019

Мне нужно создать программу, которая импортирует годовые осадки из внешнего файла, присваивает их переменной, а затем в другой функции вызывает эту переменную, чтобы вычислить среднее и общее количество осадков.

Ниже приведен код, который я пробовал, и я продолжаю получать сообщение о том, что yearRainfall не определено. Пожалуйста помоги.

def main():


    inFile = open('program9.txt', 'r')

    lineRead = inFile.readline()       # Read first record
    while lineRead != '':              # While there are more records
       words = lineRead.split()        # Split the records into substrings
       annualRainfall = float(words[0])
       print(format(annualRainfall, '.2f'))

    lineRead = inFile.readline()    # Read next record


    # Close the file.
    inFile.close() # Close file



# Call the main function.
main()

def calculations():

    low=min(annualRainfall)
    high=max(annualRainfall)
    total=sum(annualRainfall)
    average=total/12.0

    print("The smallest amount of rainfall was",low,"in",
          "the month of")
    print("The largest amount of rainfall was",high,"in",
          "the month of")
    print("Total rainfall for the year was",total,"inches")
    print("Average rainfall for the year was",average,"inches")

calculations()
...