Question

PYTHON: Write a function that takes, as an argument, the name of a file, fileName, and...

PYTHON:

Write a function that takes, as an argument, the name of a file, fileName, and an integer n between -750 and 750 (inclusive). Your program should verify that n is an integer in the correct range. If it is not, it should return the string “Your integer is out of range.” If it is in the correct range, your program should open (and read through) the file specified, and return the number of values in the file that are greater than n. Name this function greaterThanN(fileName, n).

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def greaterThanN(fileName, n):
    if -750 <= n <= 750:
        count = 0
        try:
            with open(fileName) as f:
                for line in f:
                    for word in line.strip().split():
                        number = int(word.strip())
                        if number > n:
                            count += 1
        except:
            count = 0
        return count
    else:
        return "Your integer is out of range."
Add a comment
Know the answer?
Add Answer to:
PYTHON: Write a function that takes, as an argument, the name of a file, fileName, and...
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