Question #108127

You have been tasked to write an algorithm in pseudocode, flowchart or program code to find the highest and lowest noon temperature in one year. Your algorithm should:

input the temperature at noon once a day for one year

find the highest temperature

find the lowest temperature

calculate the average temperature for the year

outputs the average, highest and lowest temperatures for the year

You should include appropriate prompts for data entry.

Expert's answer

import statistics

months = ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN',
          'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']
year = 2020
temperatureList = []

for month in months:
    if month in ['JAN', 'MAR', 'MAY', 'JUL', 'AUG', 'OCT', 'DEC']:
        length = 31
    elif month in ['APR', 'JUN', 'SEP', 'NOV']:
        length = 30
    else:
        if year // 4 == 0:
            length = 29
        else:
            length == 28

    for day in range(1, length + 1):
        print('DATE:', month, day)
        while True:
            try:
                temperature = float(input('Enter noon temperature: '))
                temperatureList.append(temperature)
                break
            except ValueError:
                print('Invalid input. Try again...')

print()

temperatureList.sort()
lowestTemperature = temperatureList[0]
highestTemperature = temperatureList[-1]
averageTemperature = statistics.mean(temperatureList)

print('Lowest Temperature:', lowestTemperature)
print('Highest Temperature:', highestTemperature)
print('Average Temperature:', averageTemperature)
LATEST TUTORIALS
APPROVED BY CLIENTS