96.9k views
0 votes
Write a program in Python to collect month-wise maximum and minimum temperatures from the user, and calculate the average maximum and minimum temperatures for the year. Use a Python object (list or dictionary) to complete the task. Your output must include the following heads:

(i) Name of the month
(ii) Minimum temperature in the month
(iii) Maximum temperature in the month
(iv) Average minimum temperature of the month
(v) Average maximum temperature of the month.

User Makogan
by
7.9k points

1 Answer

4 votes

Final answer:

To collect month-wise maximum and minimum temperatures from the user in Python, you can use a list or dictionary object. Calculate the average maximum and minimum temperatures for the year by summing up the temperature values and dividing by the number of months. Print the collected data and the average temperatures using the specified format.

Step-by-step explanation:

To collect month-wise maximum and minimum temperatures from the user in Python, you can use a list or dictionary object. Here's an example using a dictionary:

  1. Create an empty dictionary to store the temperature data.
  2. Loop through each month and ask the user to enter the maximum and minimum temperature.
  3. Store the temperature values in the dictionary with the month as the key.
  4. Calculate the average maximum and minimum temperatures for the year by summing up the temperature values and dividing by the number of months.
  5. Print the collected data and the average temperatures using the specified format.

Here's the code:

temps = {}

# Loop through each month
for month in range(1, 13):
max_temp = int(input(f"Enter the maximum temperature for month {month}: "))
min_temp = int(input(f"Enter the minimum temperature for month {month}: "))
temps[month] = {'max_temp': max_temp, 'min_temp': min_temp}

# Calculate average temperatures
total_max_temp = sum([temp['max_temp'] for temp in temps.values()])
avg_max_temp = total_max_temp / len(temps)
total_min_temp = sum([temp['min_temp'] for temp in temps.values()])
avg_min_temp = total_min_temp / len(temps)

# Print results
print(f"{'Month':12} {'Min Temp':12} {'Max Temp':12} {'Avg Min Temp':16} {'Avg Max Temp':16}")
for month, temp in temps.items():
print(f"{month:12} {temp['min_temp']:12} {temp['max_temp']:12} {avg_min_temp:16.2f} {avg_max_temp:16.2f}")

User Turkinator
by
8.0k points