0% found this document useful (0 votes)
2 views

HW4 Weather

The document outlines the requirements for a programming assignment at Bentley University, where students must create a weather data analysis tool using Python. The program should calculate and display maximum, minimum, and average temperature and humidity for specified U.S. states based on provided weather data files. Students are instructed to implement specific functions for reading state and weather data, calculating statistics, and handling user input, while adhering to academic integrity policies.

Uploaded by

sahilraut365
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

HW4 Weather

The document outlines the requirements for a programming assignment at Bentley University, where students must create a weather data analysis tool using Python. The program should calculate and display maximum, minimum, and average temperature and humidity for specified U.S. states based on provided weather data files. Students are instructed to implement specific functions for reading state and weather data, calculating statistics, and handling user input, while adhering to academic integrity policies.

Uploaded by

sahilraut365
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Bentley University Fall 2024

CS 230
Introduction to Programming with Python

Due 11:59 PM on Tuesday, December 3, 2024

Program 4: Weather Analysis


You will build a weather data analysis tool for a company that collects weather data for different states
in the United States. The company has provided you with a folder containing multiple text files with
weather data for different cities and a file containing the full names and abbreviations of all US states.

Your Python program should be able to:


• Calculate the maximum, minimum, and average temperature and humidity levels for each state.
• Accept user input to specify one or more states (by full name or abbreviation, in any case
format) and display the calculated weather information for those states.
• Use dictionaries and lists to store and manage data efficiently.
• Use functions to for better code organization and readability.

The output of the program should look like this:

DATA
A zip file, weather_data.zip, containing the data files is provided. Unzip this file and place the
unzipped folder weather_data into your project folder, where your program file will be placed.

Review the content of the folders with data – you will notice that each folder has:
• A file named us_states.txt with all US states, the format is as following shows where the
first column is the state full name and the second column is the abbreviation.

Alabama, AL
Alaska, AK
Arizona, AZ
Arkansas, AR
California, CA
Colorado, CO
Connecticut, CT
Delaware, DE
Florida, FL
Georgia, GA

• Multiple text files (cities_weather_part1.txt, cities_weather_part2.txt,


cities_weather_part3.txt, cities_weather_part4.txt, etc.) with weather

1
Bentley University Fall 2024
CS 230
Introduction to Programming with Python

data for various cities. The four columns are City Name, State Name/Abbreviation,
Average Temperature, and Average Humidity of the city, respectively.
El Paso, TX, 63.8 F, 88.3%
Detroit, Michigan, 52.5 F, 56.3%
Nashville, Tennessee, 91.4 F, 60.5%
Memphis, TN, 64.0 F, 47.7%

Review the contents and structure of these files to gain a good understanding of them.
IMPLEMENTATION
A starter file, weather-starter.py, is provided for you to begin with. Rename it as weather.py.
To produce the desired output, the program needs to perform the following steps:
1. Reading the State Info File:
• The program begins by reading the us_states.txt file, which contains the full names and
abbreviations of all U.S. states.
• Function: read_states(file_path)
o The read_states function takes the file path (“./weather_data”) containing the
us_states.txt file as an argument.
o Opens the us_states.txt file and reads each line, splitting the full state name and its
abbreviation.
o Creates and returns a dictionary state_dict that associates state abbreviations with
their full names.
o The following is an example for the returned dictionary (the following example only
contains part of the state information, the real dictionary should contain all the state
information)
▪ state_dict:

{'AL': 'Alabama', 'AK': 'Alaska', 'AZ': 'Arizona', 'AR': 'Arkansas', 'CA':


'California'}

2. Reading the Weather Data Files:


• After reading the state file, the program reads multiple weather data files from the specified
folder to gather weather information for different cities.
• Function: read_weather_files(folder_path, state_dict)
o The read_weather_files function takes the folder path and the state dictionary
(state_dict) as arguments.
o Loops through all files in the specified folder and reads each weather data file (ignoring
the us_states.txt file).
o For each line in the weather files, it extracts the city name, state (either full name or
abbreviation), average temperature, and average humidity.
o Converts the state name or abbreviation to its full name using the state_dict.
o Stores the weather data in a dictionary (weather_data) where each state (full name)
is a key, and the value is another dictionary containing two lists: temperatures and
humidities.

2
Bentley University Fall 2024
CS 230
Introduction to Programming with Python

o Returns the weather_data dictionary.


o The returned weather_data dictionary example is shown here (reformatted for
clarity). Note that the following example only contains the weather information for
some states, the real dictionary should contain the weather information for all states
recorded in the provided files:

{'California': {'humidities': [50.7, 87.0, 55.2, 89.2],


'temperatures': [99.1, 51.5, 75.7, 83.7]},
'Indiana': {'humidities': [51.8],
'temperatures': [61.6]},
'North Carolina': {'humidities': [85.8],
'temperatures': [88.8]}
}
3. Calculating Weather Statistics:
• The program then calculates the maximum, minimum, and average temperature and humidity
for each state based on the weather data collected.
• Function: calculate_weather_statistics(weather_data)
o This function takes the weather_data dictionary as an argument.
o Iterates over each state in the weather_data dictionary, extracting the lists of
temperatures and humidities.
o For each state, it computes:
▪ Maximum temperature
▪ Minimum temperature
▪ Average temperature
▪ Maximum humidity
▪ Minimum humidity
▪ Average humidity
o These computed statistics are stored in a new dictionary (weather_stats), where
each state is a key and its statistics are stored as values.
o The function returns weather_stats dictionary.
o The returned weather_stats dictionary example is shown below. Note the following
example only contains the weather information for some states, reformatted for clarity.
The real dictionary should contain the weather information for all states recorded in the
provided files:

{'California': {'AverageHumidity': 70.525,


'AverageTemperature': 77.5,
'MaxHumidity': 89.2,
'MaxTemperature': 99.1,
'MinHumidity': 50.7,
'MinTemperature': 51.5},
'Indiana': {'AverageHumidity': 51.8,
'AverageTemperature': 61.6,
'MaxHumidity': 51.8,
'MaxTemperature': 61.6,
'MinHumidity': 51.8,
'MinTemperature': 61.6},
'North Carolina': {'AverageHumidity': 85.8,
3
Bentley University Fall 2024
CS 230
Introduction to Programming with Python

'AverageTemperature': 88.8,
'MaxHumidity': 85.8,
'MaxTemperature': 88.8,
'MinHumidity': 85.8,
'MinTemperature': 88.8}}
4. User Input and Information Display:
• The program prompts the user to enter one or more state names or abbreviations (in any case
format) to display the weather statistics.
• Function: show_weather_information(states, weather_stats, state_dict)
o The program collects user input and splits it into a list of states (which can be full names
or abbreviations in any case format).
o This function is called with the list of user-input states, the weather_stats dictionary,
and the state dictionary state_dict.
o It normalizes the user input to handle case insensitivity and uses the dictionaries to
convert abbreviations to full names (or vice versa) as needed.
o Looks up the statistics for each requested state from the weather_stats dictionary.
o Prints the weather statistics (maximum, minimum, and average temperature and
humidity) for each specified state using f-string. The temperatures and humidity levels
should be with 2 decimal points. If the files do not contain the states requested, show
“No data available” for the states at the end of the output.
o Sample output

5. The Main() function:


• The main function processes the overall program:
• Function: main()
o Call read_states to read and process the state data.
o Call read_weather_files to read and process weather data from multiple files.
o Call calculate_weather_statistics to compute the weather statistics.
o Ask the user for input state names and calls show_weather_information to display
the results based on user input.
• The program ensures that all functions are executed in the correct order and that user input is
handled properly.
• The program outputs the requested weather statistics in a formatted table, displaying the
maximum, minimum, and average temperature and humidity for each state specified by the
user.

4
Bentley University Fall 2024
CS 230
Introduction to Programming with Python

REQUIREMENTS
• Replace pass in each function in the starter file with your own code.
• You DO NOT need to worry about incorrect input in this assignment. Your program will be tested
only with valid data.
• Your program may be tested on other data, so do not hardcode the input and output.

• You are prohibited from using any other Python libraries or packages that are not covered in this
class.
• The use of artificial intelligence programs to generate code or other content is strictly
prohibited. You will receive a grade of 0 and an Academic Integrity Violation for any assignments
that appear to be produced using AI tools.
HINTS

• When debugging our code, you may insert print function to print out variable values.
• When printing values of dictionaries, you can use the prettyprint module (pprint) to display the
dictionaries in an easier-to-read format. Try this program as a sample:

import pprint as pp
dict = {'California': {'temperatures': [99.1, 51.5, 75.7, 83.7],
'humidities': [50.7, 87.0, 55.2, 89.2]}, 'North Carolina':
{'temperatures': [88.8], 'humidities': [85.8]}, 'Indiana':
{'temperatures': [61.6], 'humidities': [51.8]}}
pp.pprint(dict)

Output:

{'California': {'humidities': [50.7, 87.0, 55.2, 89.2],


'temperatures': [99.1, 51.5, 75.7, 83.7]},
'Indiana': {'humidities': [51.8], 'temperatures': [61.6]},
'North Carolina': {'humidities': [85.8], 'temperatures': [88.8]}}

SUBMISSION
Test your program to make sure it works. Submit weather.py file on Brightspace.
Do not submit other files.

GRADING

• This assignment will be worth 14% toward your course grade.

• Your program will receive 0 if your file name contains space.

5
Bentley University Fall 2024
CS 230
Introduction to Programming with Python

• Your program should compile without syntax errors to receive any credit. If a part of your program is
working, you will receive partial credit, but only if the program compiles without syntax errors. Your
grade will be zero on this assignment if it contains any syntax errors.

Rubrics Points
Correctly implementing each function according to the specification 43
• read_states 3
• read_weather_files 15
• calculate_weather_statistics 10
• show_weather_information 10
• main 5
Having no code outside of function definitions except for a call to main(), and
5
no global variables
Programming style: including docstring, intro comments to functions,
2
descriptive variable names, etc.
Total 50

You might also like