Question: Please answer in Python (no imports, no list comprehensions) Given a filepath to a .txt file containing n lines (n>0) where there is one integer

Please answer in Python (no imports, no list comprehensions)

Given a filepath to a .txt file containing n lines (n>0) where there is one integer on each line, write a function that: Maps each integer to kid, teenager, adult category. If an integer is negative then map it to not a valid age. Write these mappings to a new file called files/mappings.txt on each line; - Below 14 (inclusive) is a kid - Between 15 and 19 (inclusive) is teen - Above 19 is adult Returns the sum of all valid ages (ignore negative ages)

Notes: Assume on each line there is a valid integer and files will not be empty. Assume the file always exists. You will always write to the files/mappings.txt file, and you dont need to remove the last new line in this output file.

Example: Input File files/age1.txt: 25 -3 4

Output File files/mappings.txt: adult not a valid age kid

Return Value: 25 + 4 = 29

def map_age(filepath): """ >>> map_age('files/age1.txt') 29 >>> with open('files/mappings.txt', 'r') as f: ... print(f.read().strip()) adult not a valid age kid

"""

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!