Question: Please complete following problems in python, see below for required python file code. Thank you. Here is the code of formatList.py (required in the problem):
Please complete following problems in python, see below for required python file code. Thank you.





Here is the code of formatList.py (required in the problem):
####################################################
## Format a list of items so that they are comma separated and "and"
appears # before the last item. # Parameters: # data: the list of items to format # Returns: A string containing the items from data with nice
formatting def formatList(data): # Handle the case where the list is empty if len(data) == 0: return "(None)" # Start with an empty string that we will add items to retval = ""
# Handle all of the items except for the last two for i in range(0, len(data) - 2): retval = retval + str(data[i]) + ", "
# Handle the second last item if len(data) >= 2: retval += str(data[-2]) + " and "
# Handle the last item retval += str(data[-1])
# Return the result return retval
# Run some tests if the module has not been imported if __name__ == "__main__": # Test the empty list values = [] print(values, "is formatted as", formatList(values))
# Test a list containing a single item values = [1] print(values, "is formatted as", formatList(values))
# Test a list containing two items values = [3, 4] print(values, "is formatted as", formatList(values))
# Test a list containing three items values = [-1, -2, -3]
#####################################
Thanks a lot for helping out!
Part 1: What do the Predators Eat? Your first task is to list everything that each predator eats on a single line with nice formatting. For example, if your file contains the line: Lion,Gazelle,Wildebeest,Zebra then you should output a single line for Lion that reads Lion eats Gazelle, Wildebeest and Zebra There are two parts to this printing. First is the predator name, then in between is the word eats, then what follows is a formatted list of prey eaten by that predator. Notice that commas appear after all items except the last and second last items, and that the word "and" appears between the last and second last items. You will be graded on correctly following this layout, but I have provided a module formatList.py that includes a function that will do the formatting for you all you need to do is import the function and call it. You can import this python file like you have SimpleGraphics.py previously and call the function formatList(yourList) to produce a string version of your listed formatted as required above In order to complete this and subsequent tasks you must load all of the data from the food web file into a dictionary that describes the eats relationship. The keys in the dictionary will by the names of the predators. The values in the dictionary will be lists, where each element in the list is the name of a prey animal that the predator eats
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
