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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started