Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with Intro to Computer Science 2 (Python) problem: Modify the processAnimals () function written for the second lab so that exceptions raised for

Need help with Intro to Computer Science 2 (Python) problem:

Modify the processAnimals() function written for the second lab so that exceptions raised for negative ages in the file are handled. Any animal in the file with a negative age should be ignored and the rest of the file processed as before. You must use exception handling to deal with invalid ages. Do not use branching to anticipate the problem. Instead use try-except blocks to handle exceptions that may be raised by the methods of the Animal class. The information below shows how you would call the completed function on two example files. The first file is the same as the one provided for the second lab. The second file is one that contains some negative ages. Look carefully at the file to see which lines in the file are being added to the list of animals and which lines are raising exceptions and being skipped as a result.

This is what is given to us in the template file:

image text in transcribed

Goal:

image text in transcribed

#Modified processAnimals function with InvalidAge exception def process Animals (file) animal List inFile open (file) lineList inFile readlines o inFile Close tmodify the code in loop to handle Animals being made with negative ages (refer to lab for output examples) for line in line List: data line strip ("In split newAnimal Animal (data 101, data [1], eval data [2]) animal List .append (newAnimal) newAnimal speak return animalList

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

1. Define the goals of persuasive speaking

Answered: 1 week ago

Question

Different formulas for mathematical core areas.

Answered: 1 week ago