106k views
2 votes
A simple python code that finds sum and average of rain from different cities using a dictionary?

User Selly
by
7.5k points

1 Answer

4 votes

Final answer:

To find the sum and average of rainfall from different cities using a dictionary in Python, create a dictionary, iterate over its values to calculate the sum, and then divide by the number of cities to find the average.

Step-by-step explanation:

To find the sum and average of rainfall from different cities using a dictionary in Python, you can follow these steps:

  1. Create a dictionary with city names as keys and rainfall amounts as values.
  2. Iterate over the values in the dictionary and calculate the sum of rainfall.
  3. Divide the sum by the number of cities to find the average.

Here's an example code:

rainfall = {'City1': 10.5, 'City2': 8.2, 'City3': 12.3}total_rainfall = sum(rainfall.values())average_rainfall = total_rainfall / len(rainfall)

In this example, the sum of rainfall is 30.0 inches and the average is 10.0 inches.

User Tomaso Albinoni
by
8.6k points