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:
- Create an empty dictionary to store the temperature data.
- Loop through each month and ask the user to enter the maximum and minimum temperature.
- Store the temperature values in the dictionary with the month as the key.
- 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.
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}")