Question

Can someone help me with the following problems please! 6. Write a function that accepts a...

Can someone help me with the following problems please!

6. Write a function that accepts a text file’s name and returns a. The number of uppercase letters in the file b. The number of lowercase letters in the file c. The number of digits in the file d. The number of whitespace characters in the file .

0 0
Add a comment Improve this question Transcribed image text
Answer #1
def file_stats(filename):
    upper, lower, digits, whitespace = 0, 0, 0, 0
    try:
        with open(filename, 'r') as f:
            data = f.read()
            for ch in data:
                if ch.islower():
                    lower += 1
                elif ch.isupper():
                    upper += 1
                elif ch.isdigit():
                    digits += 1
                elif ch.isspace():
                    whitespace += 1
    except FileNotFoundError:
        print(filename + " is not found!")
    return upper, lower, digits, whitespace


print(file_stats('input.txt'))
Add a comment
Answer #2

Here’s a Python function that fulfills the requested task:


python

Copy code

def analyze_file(filename):

    # Initialize counters for uppercase, lowercase, digits, and whitespaces

    uppercase_count = 0

    lowercase_count = 0

    digit_count = 0

    whitespace_count = 0


    # Open the file and read it line by line

    try:

        with open(filename, 'r') as file:

            for line in file:

                for char in line:

                    if char.isupper():

                        uppercase_count += 1

                    elif char.islower():

                        lowercase_count += 1

                    elif char.isdigit():

                        digit_count += 1

                    elif char.isspace():

                        whitespace_count += 1

        # Return the counts as a tuple

        return uppercase_count, lowercase_count, digit_count, whitespace_count

    except FileNotFoundError:

        print(f"The file {filename} was not found.")

        return None

Explanation:

Visit: https://myassignmenthelp.com/essay-help/reflective-essay.html

This function reads the given text file line by line and analyzes each character.

It uses Python’s built-in methods (.isupper(), .islower(), .isdigit(), .isspace()) to count the uppercase letters, lowercase letters, digits, and whitespace characters, respectively.

If the file is not found, it catches the FileNotFoundError and notifies the user.

For students working on coding assignments like this, balancing academic responsibilities can sometimes be challenging. In cases where the workload becomes overwhelming, seeking a reflective essay writing service or professional assistance through platforms like MyAssignmentHelp can offer support, ensuring timely submissions without compromising quality.


source: google
answered by: anonymous
Add a comment
Know the answer?
Add Answer to:
Can someone help me with the following problems please! 6. Write a function that accepts a...
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
  • PYTHON, write a function that accepts a text file's name and returns a. the number of...

    PYTHON, write a function that accepts a text file's name and returns a. the number of uppercase letters in the file b. the number of lowercase letters in the file c. the number digits in file d. the number of whitespace characters in the file

  • You're to write a C++ program that analyzes the contents of an external file called data.txt....

    You're to write a C++ program that analyzes the contents of an external file called data.txt. (You can create this file yourself using nano, that produces a simple ASCII text file.) The program you write will open data.txt, look at its contents and determine the number of uppercase, lowercase, digit, punctuation and whitespace characters contained in the file so that the caller can report the results to stdout. Additionally, the function will return the total number of characters read to...

  • i need the answer for this python question using dictionary method please!!! points) Attached to the...

    i need the answer for this python question using dictionary method please!!! points) Attached to the Exam #4 code Assignment in Tracs you ## will find a file, "text.txt". Text is stored 1 sentence per line. ## Write a program below that reads the file contents and displays ## the following: 1. Number of uppercase characters in the file 2. Number of lowercase characters in the file 3. Number of digits in the file. 4. Number of whitespace characters in...

  • Please help me with this python3 assignment. Write an iterative function, count_consonants(), that accepts a string...

    Please help me with this python3 assignment. Write an iterative function, count_consonants(), that accepts a string (that can include upper and lowercase characters) and returns the number of consonants in that string. A consonant is defined as any letter in the English alphabet other than the vowels a, e, i, o, u. For example, if you call the function as follows: count_consonants('correct horse battery staple') the function should return 20.

  • python question! points) Attached to the Exam #4 code Assignment in Tracs you ## will find...

    python question! points) Attached to the Exam #4 code Assignment in Tracs you ## will find a file, "text.txt". Text is stored 1 sentence per line. ## Write a program below that reads the file contents and displays ## the following: ## 1. Number of uppercase characters in the file ## 2. Number of lowercase characters in the file ## 3. Number of digits in the file. ## 4. Number of whitespace characters in the file ## ## print("Number of...

  • Can someone help me write a C++ program such that: 1. It counts the number of...

    Can someone help me write a C++ program such that: 1. It counts the number of alphabetic characters in a text file; this mean it ignores any character that is not alphabetic 2. Prompts the user to write the name of the file 3. Count's the number of A's, B's, C's, ..and so on For the sake of this program, both capital 'A' and lowercase 'a' and treated as equal. An output would something like this "A's = 10 B's...

  • Can someone help me write a small clean "helper" function which accepts two arguements, finds the...

    Can someone help me write a small clean "helper" function which accepts two arguements, finds the greatest common divisor, and returns it? DO NOT use recursion. This is to be written in C++. I would be greatful if you wrote a small driver main() to test it as well. Thanks in advance.

  • can someone please help me solve these problem in c++ language and leave useful comments below...

    can someone please help me solve these problem in c++ language and leave useful comments below so i can follow along and know what i am doing /view wa New Tab CSSO IDE 15 THE DIGIT SWAP PROBLEM Write a function named swapDigitPairs() that accepts a positive integer n as an input-output parameter which is changed to a new value similar to n's but with each pair of digits swapped in order. For example: int n = 482596; int old...

  • Can someone please write the following program in Python please! 1) Define a function "converT()" that...

    Can someone please write the following program in Python please! 1) Define a function "converT()" that can take a file name, reads line by line to convert temperatures, and returns line by line. Each line in a file begins with a temperature in number {integer or floating point}, followed by one of the temperature unit {"F", "f", "C", "c"}. If "F" or "f", convert the temperature to Celsius. If "C" or "c", convert the temperature to Fahrenheit. The format of...

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