Assignment 9 - ldkvd/CS101L GitHub Wiki
Welcome to the CS101L Wiki!
Kansas City Police Crime Data
This week you’ll read in a data file containing crime information for 2019. You will need to create the functions given below with the function signature given. Function signature means the definition of the name of the function as well as any parameters and return values. There will again be a unit test file to assist you. Each one will also show an example in the shell. Once you write a function you can just hit F5 and then call the function from the shell to test it with different values.
You can open the CSV files in excel to view them as rows and columns, but make sure you don’t save any changes. You can also open the file in a text editor to see how it is actually stored character by character.
Your program should only be in the if name == “main” section. Otherwise, the unit tests will fail. The program should ask the user for the file, use a try-except so if the read_in_file function throws an error you can loop and ask for a filename again. It should then output the month that has the highest crime rate. The offense that occurs the most, and then asks for an offense and output a formatted report of the zip code and how many times that offense occurs in that zip code. It should use the dictionaries created from the functions already given.
Answers:
The image above is the source code part 1/4.
The image above is the source code part 2/4.
The image above is the source code part 3/4.
The image above is the source code part 4/4.
The image above is the output.
In this code, multiple functions are defined and called. Functions allow for the same piece of code to run multiple times. It reduces clutters, complexity, and duplication of codes. It breaks down a large program into smaller, easy-to-manage components. We can call one statement (the function), rather than writing the same code over and over for each time we want to add, remove, clear a list, etc. Within some of the functions, try and except blocks are used to account for possible errors.
In the main program, a while loop is used to ask the user to enter the name of a file. The program will keep looping until a valid file is entered. The function get_max_month() is called to return the month and the highest number of crimes for that month. The program will print a message to the user displaying the month with the highest number of crimes. The function get_max_offense() is called to return the offense with the highest number of crimes and the total crimes for that offense. The program will print a message to the user displaying the offense with the highest number of crimes. Then, the function get_offense() is called which has a while loop to ask the user to input an offense. It will keep asking until the user inputs a valid output. Then, the program will output and print the formatted report of the zip code and how many times the offense occurred in that zip code by iterating through the dictionary using a for loop.