Create a program called weather-analysis.py
. The purpose of this program to practice using lists, dictionaries, and strings. This is a larger program which we will complete in increments. Ultimately, your program will help create and display a simple report of temperature readings for different cities. It will allow the user to:
Cities will be stored in a dictionary called cities
. The key will be the name of the city (e.g., “Chicago”), and the value will be a list of daily temperatures entered by the user (e.g., [70.0, 83.5, 88.1]).
cities
.cities
to test that it is working properly. Once you have confirmed this, you can remove this print statement.For example, when your program is run it will print the following. Note that the text after the colon is specified by the user when the program is run.
How many cities?: 2
Enter name of city 1: Chicago
Enter daily temperatures for Chicago (separate by spaces): 68.5 70.2 75.0
Enter name of city 2: New York
Enter daily temperatures for New York (separate by spaces): 72.3 74.1 69.8
{'Chicago': [68.5, 70.2, 75.0], 'New York': [72.3, 74.1, 69.8]}
Create a function get_average(l)
which takes a list of float point numbers and returns the average.
Create a function generate_report(d)
which takes a dictionary as an argument and returns nothing. It will print out a header “Summary Report:” followed by one line for each key/value pair of the dictionary. It will print out the key (the city) and the value (the temperature) and the average of all temperatures. Call your function using cities
as the argument to test it. Continuing with the example above, it would print out the following:
How many cities?: 2
Enter name of city 1: Chicago
Enter daily temperatures for Chicago (separate by spaces): 68.5 70.2 75.0
Enter name of city 2: New York
Enter daily temperatures for New York (separate by spaces): 72.3 74.1 69.8
Summary Report:
City: Chicago, Temperatures: [68.5, 70.2, 75.0], Average: 71.23333333333333
City: New York, Temperatures: [72.3, 74.1, 69.8], Average: 72.06666666666666
Write code that iterates over all keys in the dictionary to compute which city has the shortest name based on the number of characters. Print out the result. Continuing with the example above, after the summary report it would print:
Fun fact! Of the 2 cities given, the one with the shortest name is: Chicago
If you finished with the above tasks, you may add to your program the following features:
How many cities?: 3
Enter name for city 1:Anchorage
Enter daily temperatures for Anchorage (separated by spaces):20 10 15
Enter name for city 2:Wooster
Enter daily temperatures for Wooster (separated by spaces):60 70 50
Enter name for city 3:Miami
Enter daily temperatures for Miami (separated by spaces):90 100
Summary Report:
City: Anchorage, Temperatures: [20.0, 10.0, 15.0], Average: 15.0 ❄️
City: Wooster, Temperatures: [60.0, 70.0, 50.0], Average: 60.0
City: Miami, Temperatures: [90.0, 100.0], Average: 95.0 ☀️
Make sure you saved your work and that it runs without errors. Submit your python file to Moodle.