Starting from:

$25

Lists & File I/O soln

1 “Terrestrial Mammal Brain and Body Weights” A “.csv” file is a text file containing many rows of data in which each row consists of comma-separated values. Download the file named “BrainBodyWeightKilos.csv” from D2L. Open the file with gedit (Don’t use Excel). This file contains data collected about the weights of the bodies and brains of various 1 terrestrial mammals. Each row in the file represents a single mammal with three values: name, body weight in kilograms, brain weight in grams. Here are the first two lines and the last line of the file: African elephant,6654.0,5712.0 African giant pouched rat,1.0,6.6 ... Yellow-bellied marmot,4.05,17.0 The data shows that the African elephant weighed 6654.0 kilograms and its brain weight was 5712.0 grams. The African giant pouched rat weighed 1.0 kilogram and its brain weight was 6.6 grams. Your program will read the data file, collecting the values into lists. It will then allow the user to view information about an animal, add new animals and/or remove animals. Once the user is finished modifying the data, the program will convert all of the weights from kilograms and grams to pounds and write the data out to a new file. 1.1 Details Create a new file called “brainbodyweight.py”. In the file: 1.) Write a function called find insert position that takes two parameters. The first parameter is a string representing a terrestrial mammal’s name. The second parameter is a list of strings in alphabetical order. i.) The function returns an integer indicating the position (index) in the list of names such that if you inserted the given mammal’s name into the list at that index the list would still be in alphabetical order. * NOTE: the function does not insert the name, it does not change the names list in any way. It simply finds and returns the appropriate integer index. 2.) Write a function called write converted csv that takes four parameters. The first is the name of a file to write. The second is a list of strings (the names of mammals). The third is a list of floats (weights of mammals in kilograms). The fourth is a list of floats (brain weights of mammals in grams.) i.) The function writes a new .csv file (with the given filename) in the same format as the original “BrainBodyWeightKilos.csv” using the values in the three lists, but all kilogram values and all gram values should first be converted to pounds. (Read steps 3i and 3ii below for further clarification on the lists that will eventually be passed to this function) 3.) In main: i.) Create three empty lists to hold mammal names, body weights, and brain weights. ii.) Read the “BrainBodyWeightKilos.csv” file filling your lists with the values you read in • when you finish reading the file your name list should contain all of the names, the body weight list should contain all of the body weights and the brain weights list should contain all of the brain weights 2 • The first element in each list will be the values for the African elephant, the second element in each list will be values for the African giant pouched rat, and so on... iii.) Next you will repeatedly ask the user to: Enter animal name (or "q" to quit): Depending on the user’s response: • If the name entered by the user is not in your list of names: i. Print a message indicating the animal was not found. – For example, assuming the user entered “Bugbear” you would print: File does not contain "Bugbear". ii. Ask the user if he would like to add it. (You can assume the user will answer either ‘y’ or ‘n’.) – For example, assuming the user previously entered “Bugbear”: Add "Bugbear" to file? (y|n) – If the user enters ‘n’, do nothing (i.e. you’ll go back to asking the user to enter an animal name) – If the user enters ‘y’, ask for the body weight and brain weight: –Continuing the previous example you would ask the user for each of the two following: Enter body weight for "Bugbear" in kilograms: Enter brain weight for "Bugbear" in grams: ∗ Add the new animal’s data to your three lists. Put the values in the appropriate positions in the lists so that the animals are still all alphabetized. • If the name entered by the user is already in your list of names: i. Print the animal’s data: – For example, assuming the user entered ‘Cat’ Cat: body = 3.3kg, brain = 25.6g ii. Ask the user if he would like to delete the animal. (You can assume the user will answer either ‘y’ or ‘n’.) – For example, assuming the user previously entered “Cat”: Delete "Cat"? (y|n) – If the user enters ‘n’, do nothing (i.e. you’ll go back to asking the user to enter an animal name) – If the user enters ‘y’ delete the mammal’s data from your lists. • If the user entered ‘q’ you should: 3 – use the function you wrote for part 2 above to write the data out to a new file called “BrainBodyWeightPounds.csv” and your program will end. 4.) Verify that your documentation makes sense and that you’ve added documentation to each of your functions. 5.) Verify that your program works 6.) Upload your file to the Program 8 dropbox folder on D2L 1.2 Running Example Below is an example of what you might see in a terminal when running this program (output is in green, user input is in turquoise): Enter animal name (or "q" to quit): Bugbear File does not contain "Bugbear". Add "Bugbear" to file? (y|n) y Enter body weight for "Bugbear" in kilograms: 1000 Enter brain weight for "Bugbear" in grams: 1 Enter animal name (or "q" to quit): Goat Goat: body = 27.66kg, brain = 115.0g Delete "Goat"? (y|n) n Enter animal name (or "q" to quit): Chickenshrimp File does not contain "Chickenshrimp". Add "Chickenshrimp" to file? (y|n) n Enter animal name (or "q" to quit): Bugbear Bugbear: body = 1000.0kg, brain = 1.0g Delete "Bugbear"? (y|n) y Enter animal name (or "q" to quit): q

More products