Activity - Weather Analysis System 🌦️

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]).

Getting input from the user

  1. Prompt the user for the number of cities that they will enter.
  2. Create an initially empty dictionary called cities.
  3. Repeatedly, based on however many cities the user will enter, prompt the user to enter in the name of the city and the daily temperatures separated by spaces. Each will be a separate prompt (one for the city, one for the temperatures).
    1. The prompt for the city should read “Enter name of city X”, where X is the city number (beginning with 1).
    2. The prompt for the temperatures should include the name of the city the user just typed. For example, if the user just typed “Chicago” for the city name, this prompt would read “Enter daily temperatures for Chicago (separated by spaces): “. Note that multiple values will be specified. You will need to read the string and separate it into a list of floating point numbers. Hint: Consider how you would separate a string into multiple items delimited by spaces, convert those strings into floating point numbers, and append them to an initially empty list.
  4. Save the results that the user provides into the dictionary, using the name of the city as the key and the list of floating point numbers as the value.
  5. Once all items have been entered, print the dictionary 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]}

Analysis

  1. Create a function get_average(l) which takes a list of float point numbers and returns the average.

  2. 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
    
  3. 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
    

Optional extra (if you finish early)

If you finished with the above tasks, you may add to your program the following features:

  1. Compute which city has the longest name based on the number of characters. Print out the result.
  2. Compute which city has the highest average temperature.
  3. Compute the top 3 cities with the highest average temperature. You’ll need to enter information for several cities to test this. In the interest of time, you can test this by providing only 1 daily temperature input for each city.
  4. Compute which city has the most number of words. Print the result.
  5. Append emojis to the end of the summary report. If the daily temperature average is below 32, it should print ❄️ after the averages. If it above 80, it should print ☀️ after the averages. For example:
     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 ☀️
    

How to submit

Make sure you saved your work and that it runs without errors. Submit your python file to Moodle.