Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Database Systems Design Implementation And Management

Authors: Peter Robb,Carlos Coronel

5th Edition

061906269X, 9780619062699

More Books

Students also viewed these Databases questions

Question

1. Outline the listening process and styles of listening

Answered: 1 week ago

Question

4. Explain key barriers to competent intercultural communication

Answered: 1 week ago