Question

Question 5 (25 points): Write a function even() that takes in as a parameter a file name. The file name contains a list of integers, one on each line but there may be bad data. The function will return a list of all of the even numbers found in the file and should not crash if it encounters non-numeric input. The function should catch all file access and conversion errors (ValueError). Upload your test file to D2L. >>> print (even (q5-input.txt)) The value b could not be converted to an int [2, 4, 6, 8]
0 0
Add a comment Improve this question Transcribed image text
Answer #1

If you have any doubts, please give me comment...

Code:

def even(fname):

fp = open(fname)

evens = []

for line in fp.readlines():

line = line.strip()

try:

t = int(line)

if t%2 == 0:

evens.append(t)

except ValueError:

print("The value '%s' could not be converted to an int"%line)

return evens

print(even('q5-input.txt'))

Add a comment
Know the answer?
Add Answer to:
Question 5 (25 points): Write a function even() that takes in as a parameter a file...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT