Question

In PYTHON please: Write a function named word_stats that accepts as its parameter a string holding...

In PYTHON please:

Write a function named word_stats that accepts as its parameter a string holding a file name, opens that file and reads its contents as a sequence of words, and produces a particular group of statistics about the input. You should report the total number of words (as an integer) and the average word length (as an un-rounded number). For example, suppose the file tobe.txt contains the following text:

To be  or not  to be,    that is   the question.  

For the purposes of this problem, we will use whitespace to separate words. That means that some words include punctuation, as in "be,". For the input above, your function should produce exactly the following output. So the call of word_stats("tobe.txt") would produce the following console output:

Total words    = 10  Average length = 3.2  

Assumptions: You may assume that the input file exists and is readable.

Homework Answers

Answer #1

Here is the Python code:

def word_stats (filename):
    
    lst = []
    with open (filename, "r") as fp:
        while True:
            s =fp.readline ()
            if s == "":
                break
            lst.extend (s.split())
    
    print (lst)
    wordlengths = [len (word) for word in lst]
    
    print ("Total words = " + str(len(lst)) + " Average length = " + str (sum(wordlengths) / len(wordlengths)))
    
if __name__ == "__main__":
    filename = input ("Please enter the file name: ")
    word_stats (filename)
Know the answer?
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
Similar Questions
Q5. Code a function, named CountNumerics, that accepts a parameter of a string , named str1,...
Q5. Code a function, named CountNumerics, that accepts a parameter of a string , named str1, and returns the number of numeric characters (‘0’ .. ‘9’) + in the string. Helpful clues:   The string class has a function called length() that can be accessed by str1.length().   To access an individual character in the string; use string indexing (str1[i]) where I is an integer. Indexing of a string array starts at 0.
using Python: Write a function named safe input(prompt,type) that works like the Python input function, except...
using Python: Write a function named safe input(prompt,type) that works like the Python input function, except that it only accepts the specified type of input. The function takes two arguments: r prompt: str r type: int, float, str The function will keep prompting for input until correct input of the specified type is entered. The function returns the input. If the input was specified to be a number ( float or int), the value returned will be of the correct...
C++ create a program that: in main: -opens the file provided for input (this file is...
C++ create a program that: in main: -opens the file provided for input (this file is titled 'Lab_HW10_Input.txt' and simply has 1-10, with each number on a new line for 10 lines total) -calls a function to determine how many lines are in the file -creates an array of the proper size -calls a function to read the file and populate the array -calls a function to write out the contents of the array in reverse order *output file should...
Write a function that accepts a line of text and a single letter as input (case...
Write a function that accepts a line of text and a single letter as input (case insensitive) and returns the number of times the letter is the first character of a word. Note your program should be able to handle different cases. And check if the user input is a single letter. Example: Input text = "When the SUN rises at dawn, the chicken flies into the window." Input letter = "t" Output = "The letter t has appeared 3...
Python. create a function that takes a string as a parameter and prints out the following...
Python. create a function that takes a string as a parameter and prints out the following details as the example output below. If the string is "Hello, my name is Starling, Clarice. My, my, my, nice to meet you." The output would be: Unique words: 10 Number of commas: 5 Number of periods: 2 Number of sentences: 2 (HINT: Use the following string methods: split, lower, count, replace, format, and use the following functions: set, len. You may need to...
[Python] Write a function named "total_population" that takes a string then a list as parameters where...
[Python] Write a function named "total_population" that takes a string then a list as parameters where the string represents the name of a CSV file containing city data in the format "CountryCode,CityName,Region,Population,Latitude,Longitude" and the second parameter is a list where each element is itself a list containing 3 strings as elements representing the CountryCode, CityName, and Region in this order. Return the total population of all cities in the list. Note that the city must match the country, name, and...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first,...
Q1) Write a Python function partial_print, which takes one parameter, a string, and prints the first, third, fifth (and so on) characters of the strings, with each character both preceded and followed by the ^ symbol, and with a newline appearing after the last ^ symbol. The function returns no value; its goal is to print its output, but not to return it. Q2) Write a Python function called lines_of_code that takes a Path object as a parameter, which is...
write a python program that include a function named activity_selection() and take in two arguments, first...
write a python program that include a function named activity_selection() and take in two arguments, first one would be the number of tasks and the second argument would be a list of activities. Each activity would have an activity number, start time and finish time. Example activity_selection input and output: activity_selection (11, [[1, 1, 4 ], [2, 3, 5], [3, 0, 6], [4, 5, 7], [5, 3, 9], [6, 5, 9],[7, 6, 10], [ 8, 8, 11], [ 9, 8,...
PLEASE HURRY, will upvote if correct python Write a method that prints characters using the following...
PLEASE HURRY, will upvote if correct python Write a method that prints characters using the following header: def printChars(ch1,ch2): this function prints out the characters between ch1 and ch2 inclusive. Assumption is that ch2 is always bigger than c1 and both lower cases. also Write a function that accepts a list as an argument and prints the number of occurrence of each item def itemOccurence(myList): i.e. input ("Hello",123, 342, 123, "Hello",123) output: 2 times "Hello" 3 times 123 1 time...
So, i have this code in python that i'm running. The input file is named input2.txt...
So, i have this code in python that i'm running. The input file is named input2.txt and looks like 1.8 4.5 1.1 2.1 9.8 7.6 11.32 3.2 0.5 6.5 The output2.txt is what i'm trying to achieve but when the code runs is comes up blank The output doc is created and the code doesn't error out. it should look like this Sample Program Output 70 - 510, [semester] [year] NAME: [put your name here] PROGRAMMING ASSIGN MENT #2 Enter...